一个多项式可以表示为一组数对,数对中第一个数始终为整数,且唯一,表示多项式的次数,另一数表示为对应的系数且不为0。输入两组数对,每组以0 0作为结束,实现对两个多项式的加法并按降幂输出结果数对
输入
每行输入一个数对,以空格为分隔符,以0 0结束
输出
每行输出一个数对,以空格为分隔符
样例输入
5 12
3 8
1 2
15 5
0 10
0 0
3 12
30 1
15 5
0 0
样例输出
30 1
15 10
5 12
3 20
1 2
0 10
#include<iostream>
using namespace std;
int main(){
int a[100];int b[200];
int c;int d;
int count=0;
while(cin>>a[count]>>b[count]&&(a[count]!=0||b[count]!=0)){
count++;
}
while(cin>>c>>d&&(c!=0||d!=0)){
int k=0;
for(int i=0;i<count;i++){
if(c==a[i]){
b[i]=b[i]+d;
k=1;
break;
}
}
if(k==0){
a[count]=c;
b[count]=d;
count++;
}
}
for(int i=1;i<count;i++){
int j=i;
int temp1=a[i];
int temp2=b[i];
while(j>0&&temp1>a[j-1]){
a[j]=a[j-1];
b[j]=b[j-1];
j--;
}
a[j]=temp1;
b[j]=temp2;
}
for(int i=0;i<count;i++){
if(i!=count-1)
cout<<a[i]<<" "<<b[i]<<endl;
else
cout<<a[i]<<" "<<b[i];
}
return 0;
}