A+B for Polynomials

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


 1 #include <cstdlib>
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cctype>
 5 #include <cstring>
 6 #include <cmath>
 7 #include <vector>
 8 #include <list>
 9 #include <deque>
10 #include <map>
11 #include <stack>
12 #include <set>
13 #include <queue>
14 #include <string>
15 #include <algorithm>
16 
17 
18 
19 using namespace std;
20 
21 double a[1000+4];
22 double b[1000+4];
23 double c[1000+4];
24 
25 
26 int main(int argc, char *argv[])
27 {
28   int k1,k2;
29   int i,j,k;
30   
31   for(i=0;i<=1000;i++)
32   {
33                       a[i]=b[i]=c[i]=0;
34   }
35   
36   
37   
38   scanf("%d",&k1);
39   
40   for(i=0;i<k1;i++)
41   {
42                    int e;
43                    double coe;
44                    scanf("%d%lf",&e,&coe);
45                    
46                    
47                    a[e]=coe;
48   }
49   
50   scanf("%d",&k2);
51   
52   for(i=0;i<k2;i++)
53   {
54                    int e;
55                    double coe;
56                    scanf("%d%lf",&e,&coe);
57                    b[e]=coe;
58   }
59   
60   
61   for(i=0;i<=1000;i++)
62   {
63                       c[i]=a[i]+b[i];
64   }
65   
66   
67   int num=0;
68   
69   for(i=0;i<=1000;i++)
70   {
71                       if(c[i]!=0.0)
72                       {num++;}
73   }
74   
75   printf("%d",num);
76   
77   for(i=1000;i>=0;i--)
78   {
79                       if(c[i]!=0.0)
80                       {
81                                    printf(" %d %.1lf",i,c[i]);
82                       }
83   }
84   
85   puts("");
86                    
87     
88     
89     
90     
91         return EXIT_SUCCESS;
92 }

 


posted @ 2012-05-30 21:41  cseriscser  阅读(504)  评论(0编辑  收藏  举报