【问题描述】
一个多项式可以表示为一组数对,数对中第一个数始终为整数,且唯一,表示多项式的次数,另一数表示为对应的系数且不为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
【大致思路】思路很简单,map yyds,直接看代码就可以了,但是有一个坑,就是记得多项式系数为0的时候不用输出,反正我是日常踩坑了
#include <iostream>
#include <map>
using namespace std;
int main(){
int a,b;//a为次数,b为系数
map<int,int> per;
while(cin>>a>>b&&(a!=0||b!=0))
per[a]+=b;
while(cin>>a>>b&&(a!=0||b!=0))
per[a]+=b;
for(map<int,int>::iterator it=per.end();it!=per.begin();it--)
{
if(it!=per.end()&&it->second!=0)
cout<<it->first<<" "<<it->second<<endl;
}
if(per.begin()->second!=0)
cout<<per.begin()->first<<" "<<per.begin()->second;
}