-
Notifications
You must be signed in to change notification settings - Fork 87
/
1002.c
45 lines (36 loc) · 876 Bytes
/
1002.c
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
#include <stdio.h>
#include <math.h>
typedef struct Poly {
double coef;
int exp;
} Poly[20];
int main()
{
int KA, KB, Ksum = 0;
Poly A, B, sum;
scanf("%d", &KA);
for (int i = 0; i < KA; i++)
scanf("%d %lf", &A[i].exp, &A[i].coef);
scanf("%d", &KB);
for (int i = 0; i < KB; i++)
scanf("%d %lf", &B[i].exp, &B[i].coef);
int i = 0, j = 0;
while (i < KA || j < KB) {
if (i == KA || (j < KB && A[i].exp < B[j].exp)) {
sum[Ksum].exp = B[j].exp;
sum[Ksum].coef = B[j++].coef;
} else if (j == KB || (i < KA && A[i].exp > B[j].exp)) {
sum[Ksum].exp = A[i].exp;
sum[Ksum].coef = A[i++].coef;
} else {
sum[Ksum].exp = A[i].exp;
sum[Ksum].coef = A[i++].coef + B[j++].coef;
}
if (fabs(sum[Ksum].coef) >= 0.05)
Ksum++;
}
printf("%d", Ksum);
for (int i = 0; i < Ksum; i++)
printf(" %d %.1lf", sum[i].exp, sum[i].coef);
return 0;
}