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 Input2 1 2.4 0 3.2 2 2 1.5 1 0.5Sample Output
3 2 1.5 1 2.9 0 3.2
多项式相加,输入一共2行,每行第一个表示项数,sample表示 第一个多项式有2项,为 2.4x+3.2,第二个多项式为两项,为 1.5x^2+0.5x,两个多项式相加以后为3项,为1.5x^2+2.9x+3.2,输入的一个多项式最多10项,但多项式最高幂为1000,还有系数保留一位小数;
// // main.cpp // PAT // // Created by hqr on 14-2-7. // Copyright (c) 2014年 hqr. All rights reserved. // #include <iostream> using namespace std; int nA,nB,nC=0; double input; double fAns[1001]; int main(int argc, const char * argv[]) { // insert code here... cin >> nA; for(int i=0;i<nA;i++) { cin>>nB>>input; fAns[nB]+=input; } cin >> nA; for(int i=0;i<nA;i++) { cin>>nB>>input; fAns[nB]+=input; } for(int i=0;i<=1000;i++) { if (fAns[i]!=0) { nC++; } } cout << nC; for(int i=1000;i>=0;i--) { if (fAns[i]!=0) { printf(" %d %.1lf",i,fAns[i]); } } cout<<endl; return 0; }