joj1011
1011: If only I had a Venn diagram
Result | TIME Limit | MEMORY Limit | Run Times | AC Times | JUDGE |
---|---|---|---|---|---|
3s | 524288K | 3990 | 1595 | Standard |
The symmetric difference of two sets is the set of elements belonging to one but not both of the two sets. For example, if we have two sets A = {1,2,3,4,5} and B = {3,4,5,6,7,8}, then the symmetric difference of A and B is the set {1,2,6,7,8}.
Given two sets of positive integers, display their symmetric difference.
Input
A positive integer will denote the number of cases. Both sets will be input from a single line. A zero (0) marks the end of each set. There will be no more than 20 numbers in each set, and no number within a set will be repeated. Each number in a set is a positive integer less than 65535.
Output
The set of integers making up the symmetric difference of the two sets. The numbers within a set may be sorted from smaller to bigger.
Sample Input
3 1 2 3 4 5 0 3 4 5 6 7 8 0 1 2 3 0 1 2 3 0 129 34 5 6 7 0 129 0
Sample Output
{1,2,6,7,8} {} {5,6,7,34}
This problem is used for contest: 50
Submit / Problem List / Status / Discuss
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 int cmp(const void *a, const void *b)
6 {
7 return *(int *)a - *(int *)b;
8 }
9
10 void output(int a[], int t)
11 {
12 int b[50];
13 memset(b, 0, sizeof(b));
14 int i;
15
16 for (i=0; i<t-1; i++)
17 {
18 if (a[i] == a[i+1])
19 {
20 b[i] = b[i+1] = 1;
21 }
22 }
23 int flag = 1;
24 printf("{");
25 for (i=0; i<t; i++)
26 {
27 if (flag && 0 == b[i])
28 {
29 flag = 0;
30 printf("%d", a[i]);
31 }
32 else
33 {
34 if (0 == b[i])
35 {
36 printf(",%d", a[i]);
37 }
38 }
39 }
40 printf("}\n");
41
42 }
43
44 int main(void)
45 {
46 int sample, val;
47 int a[50];
48
49 freopen("in.txt", "r", stdin);
50
51 while (scanf("%d", &sample) == 1)
52 {
53 for (int i=0; i<sample; i++)
54 {
55 int j = 0;
56 while(scanf("%d", &val), val)
57 {
58 a[j++] = val;
59 }
60
61 while (scanf("%d", &val), val)
62 {
63 a[j++] = val;
64 }
65
66 qsort(a, j, sizeof(int), cmp);
67
68 output(a, j);
69 }
70 }
71
72 return 0;
73 }