A1002. A+B for Polynomials (25)

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 // hahaha.cpp : 定义控制台应用程序的入口点。
 2 //
 3 
 4 #include <stdafx.h>
 5 #include <stdio.h>
 6 #include <iostream>
 7 #include <vector>
 8 #include <map>
 9 #include <string>
10 #include <cstdio>
11 #include <set>
12 #include <algorithm>
13 #include <string.h>
14 using namespace std;
15 const int maxn=1020;
16 double p[maxn]={0};
17 
18 int main()
19 {
20     int k,n;
21     double a;
22     scanf("%d",&k);
23     for(int i=0;i<k;i++)
24         {
25         scanf("%d %lf",&n,&a);
26         p[n]+=a;
27         }
28     scanf("%d",&k);
29     for(int i=0;i<k;i++)
30         {
31         scanf("%d %lf",&n,&a);
32         p[n]+=a;
33         }
34     int count=0;
35     for(int i=0;i<maxn;i++)
36         {
37         if(p[i]!=0)count++;
38         }
39     printf("%d",count);
40     for(int i=maxn-1;i>=0;i--)
41         {
42         if(p[i]!=0)printf(" %d %.1f",i,p[i]);
43         }
44     return 0;
45 }

 

posted @ 2015-03-16 12:29  Joilee  阅读(146)  评论(0编辑  收藏  举报