-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA1002.cpp
More file actions
62 lines (61 loc) · 1.12 KB
/
A1002.cpp
File metadata and controls
62 lines (61 loc) · 1.12 KB
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
58
59
60
61
62
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
struct pol{
int zhi;
float xi;
};
int main()
{
int n1,n2,k=0;
cin>>n1;
pol *a=new pol[n1];
for(int i=0;i<n1;i++)
cin>>a[i].zhi>>a[i].xi;
cin>>n2;
pol *b=new pol[n2];
for(int i=0;i<n2;i++)
cin>>b[i].zhi>>b[i].xi;
pol *c=new pol[n1+n2];
for (int i=0,j=0;i<n1||j<n2;)
{
if(i>=n1)
{
c[k]=b[j];
j++;
}
else if(j>=n2)
{
c[k]=a[i];
i++;
}
else if(a[i].zhi>b[j].zhi)
{
c[k]=a[i];
i++;
}
else if(a[i].zhi<b[j].zhi)
{
c[k]=b[j];
j++;
}
else if(a[i].zhi==b[j].zhi)
{
c[k]=a[i];
c[k].xi=c[k].xi+b[j].xi;
i++;j++;
}
if(c[k].xi!=0)
k++;
}
cout<<k;
for(int i=0;i<k;i++)
{
if(c[i].xi!=0)
printf(" %d %.1f",c[i].zhi,c[i].xi);
// cout<<c[i].zhi<<" "<<c[i].xi<<" ";
}
return 0;
}