1002. A+b for polynomials (+)Time limit MS
Memory Limit 65536 KB
Code length limit 16000 B
Program Standard author CHEN, Yue
This is supposed to find a+b where A and B are both polynomials.
Input
Each input file contains the one test case. Each case occupies 2 lines, and all 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 Coeffi Cients, 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 on one line, with the same format as the input. Notice that there must is NO extra space at the end of each line. 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 is the number of non-0 items of the polynomial, the exponent n and the coefficient an of two polynomial are given, and the values of two polynomial are calculated.
Note that the last possible result is full 0, to retain one decimal place, 0 items not output and no spaces at the end of the output.
#include <bits/stdc++.h> using namespace std;
#define MAXN 1010 #define INF 0xfffffff int main () {#ifdef Online_judge #else freopen ("F:/cb/read.txt", "R", stdin);
Freopen ("F:/cb/out.txt", "w", stdout);
#endif Ios::sync_with_stdio (FALSE);
Cin.tie (0);
Double A[MAXN],B[MAXN],ANS[MAXN];
memset (A,0,sizeof (a));
Memset (b,0,sizeof (b));
memset (ans,0,sizeof (ans));
int k,ma=-1,mb=-1,m=-1,mm=-1,cnt=0,mi=inf;
cin>>k;
while (k--) {int x;
Double y;
cin>>x>>y;
Ma=max (MA,X);
A[x]=y;
} cin>>k;
while (k--) {int x;
Double y;
cin>>x>>y;
Mb=max (MB,X);
B[x]=y;
} m=max (MA,MB);
for (int i=m; i>=0;-I.) {if (a[i]==0&&b[i]==0) continue;
if (a[i]+b[i]!=0) {++cnt;
Mm=max (Mm,i);
Mi=min (Mi,i);
Ans[i]=a[i]+b[i]; }} cout<<cnt;
if (cnt>0) {cout<< ""; for (int i=m; i>mi;-i) if (ans[i]!=0) cout<<i<< "" <<setiosflags (Ios::fixe
d) <<setprecision (1) <<ans[i]<< "";
cout<<mi<< "" <<setiosflags (ios::fixed) <<setprecision (1) <<ans[mi]<<endl;
} return 0;
}