题目
This time, you are supposed to find A+B where A and B are two polynomials.
输入
Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial: K N1 aN1 N2 aN2 ... NK aNK
where K is the number of nonzero terms in the polynomial, Ni and aNi (i=1,2,⋯,K) are the exponents and coefficients, respectively. It is given that1≤K≤10,0≤NK<⋯<N2<N1≤1000.
输出
For each test case you should output the sum of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate to 1 decimal place.
思路
这就是多项式的加法,需要一个长度为1000以上的数组储存多项式,也就是最后需要输出的。你的输入用一个scanff函数来实现,使用两次分别输入两个多项式,此外scanff还有多项式相加的功能。然后就是数出数组中不为0的元素个数,这就是多项式的项数,最后输出
//foreverking
#include <vector>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
using namespace std;
double ans[1001];
int m,n;
double num;
void scanff(){
cin >> n;//重复使用n,输入第二组
for (int i = 0; i < n; i++){
/* code */
cin >> m >> num;//m是多项式第一项的指数,num是系数
ans[m] += num;
}
}
int main(){
scanff();
scanff();
int cnt = 0;//计数器
for(int i = 0; i < 1001; i++){
//遍历
if(ans[i] != 0)
cnt++;//项数
}
cout << cnt;
for(int i = 1000; i >= 0; i--){
if(ans[i] != 0)
printf(" %d %.1lf",i,ans[i]);
}
return 0;
}