This time, you are supposed to find A+B where A and B are two polynomials.
Input
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 that 1 <= K <= 10,0 <= NK < … < N2 < N1 <=1000.
Output
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.
Sample Input
2 1 2.4 0 3.2
2 2 1.5 1 0.5
Sample Output
3 2 1.5 1 2.9 0 3.2
/*
题意:K表示输入 非零项个数 N1为指数 aN1为系数
A和B指数相同的项 相加 然后按照输入格式 输出
其实就是计算多项式A+B的和
*/
#include <iostream>
#include <iomanip>
using namespace std;
const int maxn = 1005;
double res[maxn] = {0.0}; //res[n]表示幂次为n的项的系数,初始值为0
int main(){
int k, e; //k表示多项式中的项数 e为指数
double c; //c为系数
int count = 0; //统计项数
for(int i = 0; i < 2; i++){ //注意控制输入格式
cin >> k;
for(int i=0; i<k; i++){
cin >> e >> c;
res[e] += c; //先读入第一个多项式,再读入第二个,对应系数直接加到第一个多项式上去
}
}
for(int i = 0; i < maxn; i++){ //不能再读入过程中统计count,因为可能出现正负抵消的问题:两个多项式的某一个相同幂次的项刚好为相反数,相加后直接为0了
if(res[i] != 0) count++;
}
cout << count; //先输出项数
for(int i = maxn; i >= 0; i--){
if(res[i] != 0) cout << fixed << setprecision(1) << " " << i << " " << res[i]; //i作为整形变量 似乎不受setprecision的影响
//if(res[i] != 0) printf(" %d %.1f", i, res[i]); //输出不为0的项即可
}
return 0;
}