Problem Description
Problem Analysis
It’s also a pretty interesting problem
But I don’t think it took much effort back then
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
| #include<stdio.h>
int main() {
int t,i,k,j,n,m,x,y;
int A[120],B[120],R[120];
while(scanf("%d %d",&n,&m),n!=0||m!=0) {
for(i=0; i<n; i++)
scanf("%d",&A[i]);
for(i=0; i<m; i++)
scanf("%d",&B[i]);
j=y=0;
for(i=0; i<n; i++) {
x=0;
for(k=0; k<m; k++) {
if(A[i]==B[k])
x=1;
}
if(x==0) {
y=1;
R[j]=A[i];
j++;
}
}
if(y==1) {
for(i=0; i<j; i++) {
for(k=0; k<j; k++) {
if(R[i]<R[k]) {
t=R[i];
R[i]=R[k];
R[k]=t;
}
}
}
for(i=0; i<j; i++)
printf("%d ",R[i]);
printf("\n");
} else
printf("NULL\n");
}
return 0;
}
|
========20160531 Addendum========
Let’s simplify the idea
for each element in set A
check whether this element exists in set B
if it does, no need to output it
Finally, check whether any element was output
if none was, then output NULL
Note that the final output must be sorted
so you can use a separate array to record the result
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
| #include<stdio.h>
#include<algorithm>
using namespace std;
int a[120],b[120],res[120];
int main() {
int n,m;
while(scanf("%d %d",&n,&m),n||m) {
for(int i=0; i<n; i++)
scanf("%d",&a[i]);
for(int i=0; i<m; i++)
scanf("%d",&b[i]);
int cnt=0;
for(int i=0; i<n; i++) {
int j;
for(j=0; j<m; j++) {
if(b[j]==a[i]) {
break;
}
}
if(j>=m) {
res[cnt++]=a[i];
}
}
if(cnt==0)
printf("NULL");
else {
sort(res,res+cnt);
for(int i=0; i<cnt; i++)
printf("%d ",res[i]);
}
printf("\n");
}
return 0;
}
|