题目
【问题描述】
一个多项式可以表示为一组数对,数对中第一个数始终为整数,且唯一,表示多项式的次数,另一数表示为对应的系数且不为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>
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a,b;
int flag=0;
map<int,int>mp;
while(cin>>a>>b)
{
if(a==0&&b==0) flag+=1;
if(flag==2) break;
mp[a]+=b;
}
map<int,int>::iterator it;
for(it=mp.end();it!=mp.begin();it--)
{
if(it->second!=0)
cout<<it->first<<" "<<it->second<<endl;
}
if(mp.begin()->second!=0)
cout<<mp.begin()->first<<" "<<mp.begin()->second<<endl;
return 0;
}
注意
1.mp倒序时 it - -
2.对mp.begin( )的操作