一道水题,练习链表用
#include<bits/stdc++.h>
using namespace std;
typedef struct number
{
int xi;
int zhi;
struct number *next;
}number,*linklist;
linklist l;
void init()
{
int xi,zhi;
l=(number*)malloc(sizeof(number));
l->next=NULL;
number *a,*b;
a=l;
while(cin>>xi>>zhi)
{
b=(number*)malloc(sizeof(number));
b->xi=xi;
b->zhi=zhi;
b->next=NULL;
a->next=b;
a=b;
if(zhi==0)
break;
}
}
void solve()
{
number *a;
a=l->next;
while(a!=NULL)
{
a->xi=a->xi*a->zhi;
if(a->zhi!=0)
a->zhi-=1;
a=a->next;
}
}
void print()
{
number *a;
a=l->next;
int flag=0,x=0;
while(a!=NULL)
{
if(a->xi!=0)
{
flag=1;
if(x)
cout<<' ';
cout<<(a->xi)<<' '<<(a->zhi);
x++;
}
a=a->next;
}
if(flag==0)
cout<<0<<' '<<0;
}
int main()
{
init();
solve();
print();
}
ps:代码仅供参考,请勿抄袭