A+B for Polynomials(PATA-1002)
题面
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 that 1≤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.
样例输入
12 1 2.4 0 3.2
22 2 1.5 1 0.5
样例输出
13 2 1.5 1 2.9 0 3.2
提示
无
思路
代码
1#define Sg(u) ((u)>eps?1:((u)<-eps?-1:0))
2#define Abs(u) (Sg(u)>=0?(u):-(u))
3#define Ze(u) (!Sg(u))
4#define Eq(u,v) (Ze((u)-(v)))
5const double eps = 1e-6;
6double a[1005];
7
8int main()
9{
10 int an; scanf("%d", &an);
11 for(int i=0; i<an; i++){
12 int n; scanf("%d", &n);
13 scanf("%lf", &a[n]);
14 }
15
16 int bn; scanf("%d", &bn);
17 for(int i=0; i<bn; i++){
18 int n; double x;
19 scanf("%d %lf", &n, &x);
20 a[n] += x;
21 }
22
23 int ans = 0;
24 for(int i=1000; i>=0; i--){
25 if(Sg(a[i]))
26 ans++;
27 }
28 printf("%d", ans);
29
30 for(int i=1000; i>=0; i--){
31 if(Sg(a[i]))
32 printf(" %d %.1lf", i, a[i]);
33 }
34 printf("\n");
35
36 return 0;
37}