/
1002.cpp
57 lines (51 loc) · 1.17 KB
/
1002.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//A+B for Polynomials
//because N1 and N2 is between 0-1000, so be careful of the MAXK is 1001
//we use two lines of array to store the Polynomials
#include<stdio.h>
#define MAXK 1001
double coefficients[2][MAXK];//store two lines of exponents and coefficients,index is exponent and value is coefficient
int K;
void init();
void process();
int main()
{
init();
process();
return 0;
}
void init()
{
//initialize coefficients array
for(int i=0;i<2;i++)
for(int j=0;j<MAXK;j++)
coefficients[i][j]=0;
int temp;
for(int i=0;i<2;i++)//put the number to two int[]
{
scanf("%d",&K);
for(int j=0;j<K;j++)
{
scanf("%d",&temp);
scanf("%lf",&coefficients[i][temp]);
}
}
return;
}
void process()
{
int count=0;
for (int i=0; i<MAXK; i++)
{
coefficients[0][i]+=coefficients[1][i];
if(coefficients[0][i]!=0)//recording the count of the nonezero terms
count++;
}
printf("%d",count);
for(int i=MAXK-1;i>=0;i--)
{
if(coefficients[0][i]!=0)
printf(" %d %.1lf",i,coefficients[0][i]);
}
printf("\n");
return;
}