-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12.c
84 lines (82 loc) · 1.62 KB
/
12.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include<stdio.h>
#include<stdlib.h>
typedef struct matrix
{
int ra,co,da;
}*sparse;
int main()
{
int t1,t2;
scanf("%d%d",&t1,&t2);
sparse a,b,c;
a=(sparse)calloc(t1,sizeof(struct matrix));
b=(sparse)calloc(t2,sizeof(struct matrix));
c=(sparse)calloc(t1+t2,sizeof(struct matrix));
if((NULL==a)||(NULL==b)||(NULL==c))
{
exit(1);
}
int i;
for(i=0;i<t1;i++)
{
scanf("%d%d%d",&a[i].ra,&a[i].co,&a[i].da);
}
for(i=0;i<t2;i++)
{
scanf("%d%d%d",&b[i].ra,&b[i].co,&b[i].da);
}
int cnt,j;
sparse tmp;
for(i=0,j=0,cnt=0;i<t1 && j< t2;)
{
if(a[i].ra == b[j].ra && a[i].co == b[j].co)
{
if(a[i].da+b[j].da!=0)
{
c[cnt].ra=a[i].ra;
c[cnt].co=a[i].co;
c[cnt].da=a[i].da+b[j].da;
cnt++;
}
i++; j++;
}
else
{
if(a[i].ra < b[j].ra || (a[i].ra == b[j].ra && a[i].co < b[j].co))
{
tmp=a+i;
i++;
}
else
{
tmp=b+j;
j++;
}
c[cnt].ra=tmp->ra;
c[cnt].co=tmp->co;
c[cnt].da=tmp->da;
cnt++;
}
}
if(i==t1)
{
tmp=b;
i=j;
t1=t2;
}
else
{
tmp=a;
}
for(;i<t1;i++)
{
c[cnt].ra=(tmp+i)->ra;
c[cnt].co=(tmp+i)->co;
c[cnt].da=(tmp+i)->da;
}
for(i=0;i<cnt;i++)
{
printf("%d %d %d\n",c[i].ra,c[i].co,c[i].da);
}
return 0;
}