这道题我发现用map还是简单的
O(n)时间复杂度 多项式的加法
作者 张瑞霞
单位 桂林电子科技大学
用链表表示多项式,并实现多项式的加法运算
输入格式:
输入在第一行给出第一个多项式POLYA的系数和指数,并以0,0 结束第一个多项式的输入;在第二行出第一个多项式POLYB的系数和指数,并以0,0 结束第一个多项式的输入。
输出格式:
对每一组输入,在一行中输出POLYA+POLYB和多项式的系数和指数。
输入样例:
5,0 2,1 1,6 8,15 0,0
-2,1 3,6 4,8 0,0
输出样例:
5,0 4,6 4,8 8,15
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
代码展示:
#include<bits/stdc++.h>
using namespace std;
int main(){
int x,y;
char c;
map<int,int>m1;
while(cin>>x>>c>>y){
if(x==0&&y==0)
continue;
m1[y]+=x;
}
for(auto it=m1.begin();it!=m1.end();it++){
if(it->second!=0)
cout<<it->second<<","<<it->first<<" ";
}
return 0;
}