1002. A+B for Polynomials

1002. A+B for Polynomials (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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<stdio.h>
 2 #include<math.h>
 3 #include<stdlib.h>
 4 #include<string.h>
 5 
 6 int main()
 7 {
 8     double a[1010], b[1010];
 9     memset(a, 0, sizeof(a));
10     memset(b, 0, sizeof(b));
11     int ka, kb, i, j, x, y, maxa, maxb;
12     scanf("%d", &ka);
13     scanf("%d", &x);
14     scanf("%lf", &a[x]);
15     maxa = x;
16     for(i = 1; i < ka; i++)
17     {
18         scanf("%d", &x);
19         scanf("%lf", &a[x]);
20     }
21     scanf("%d", &kb);
22     scanf("%d", &y);
23     scanf("%lf", &b[y]);
24     maxb = y;
25     for(i = 1; i < kb; i++)
26     {
27         scanf("%d", &y);
28         scanf("%lf", &b[y]);
29     }
30     if(maxa < maxb)
31     {
32         int temp = maxa;
33         maxa = maxb;
34         maxb = temp;
35     }
36     for(i = maxa; i >= 0; i--)
37     {
38         a[i] += b[i];
39     }
40     int count = 0;
41     for(i = maxa; i >= 0; i--)
42     {
43         if(a[i] != 0)
44             count++;
45     }
46     printf("%d", count);
47     for(i = maxa; i >= 0; i--)
48     {
49         if(a[i] != 0)
50         {
51             printf(" %d %.1f", i, a[i]);
52         }
53     }
54     printf("
");
55     return 0;
56 }
原文地址:https://www.cnblogs.com/yomman/p/4268314.html