This time, you are supposed to find A+B where A and B are two polynomials.
Input Specification:
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 (,) are the exponents and coefficients, respectively. It is given that 1,0.
Output Specification:
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
#include <the iostream> #include <Vector> #include <algorithm> the using namespace STD; struct poly { int Expo; Double Coef; }; BOOL CMP (poly PO1, PO2 poly) { IF (po1.expo> po2.expo) return to true ; return to false ; } int main () { / * * Note points: 1. the first index, the coefficient 2 is not intended to indicate whether the question is descending polynomial, so as to try descending * * / / * * input data * / int <poly> T; poly TEMP; Vector vec1; Vector <poly> vec2; Vector <poly> RES; CIN >> T; the while (T-- ) { CIN >> temp.expo; CIN >> temp.coef; vec1.push_back (TEMP); } CIN >> T; the while (T-- ) { CIN >> temp.expo; CIN >> temp.coef; vec2.push_back (TEMP); } / * * sort, did not say that Italy ordered * / Sort (vec1 .begin (), vec1.end (), cmp); sort(vec2.begin(),vec2.end(),cmp); /**计算*/ int i=0,j=0; while(true){ if(vec1[i].expo==vec2[j].expo){ poly temp; temp.expo=vec1[i].expo; temp.coef=vec1[i].coef+vec2[j].coef; if(temp.coef!=0) res.push_back(temp); /**coef不为0时相加!!!!!*/ i++;j++; }else if(vec1[i].expo>vec2[j].expo){ res.push_back(vec1[i]); i++; }else{ res.push_back(vec2[j]); j++; } if(i==(vec1.size())){ while(j!=(vec2.size())){ res.push_back(vec2[j]); j++; } break; } if(j==(vec2.size())){ while(i!=(vec1.size())){ res.push_back(vec1[i]); i++; } BREAK ; } IF (I == (vec1.size ()) && J == (vec2.size ())) { BREAK ; } } COUT << res.size (); for ( int I = 0 ; I <res.size (); I ++ ) { the printf ( " % D% .1f " , RES [I] .expo, RES [I] .coef); / * * It should be noted that a floating point decimal! ! ! ! ! * / } System ( " PAUSE " ); return 0 ; }
Logic is correct, there has been a lot of errors:
1. The addition coefficient is not zero is not considered
2. Float is not considered one decimal
It should be noted that the examination