7-2 找出不是两个数组共有的元素 (20 分)
给定两个整型数组,本题要求找出不是两者共有的元素。
输入格式:
输入分别在两行中给出两个整型数组,每行先给出正整数N(≤),随后是N个整数,其间以空格分隔。
输出格式:
在一行中按照数字给出的顺序输出不是两数组共有的元素,数字间以空格分隔,但行末不得有多余的空格。题目保证至少存在一个这样的数字。同一数字不重复输出。
输入样例:
10 3 -5 2 8 0 3 5 -15 9 100
11 6 4 8 2 6 -5 9 0 100 8 1
输出样例:
3 5 -15 6 4 1
1 #include<stdio.h> 2 int main() 3 { 4 int a[20], b[20], c[20]; 5 int m, n, i, j, k=0; 6 scanf("%d", &m); 7 for(i=0; i<m; i++) 8 scanf("%d", &a[i]); 9 scanf("%d", &n); 10 for(i=0; i<n; i++) 11 scanf("%d", &b[i]); 12 for(i=0; i<m; i++){ 13 for(j=0; j<n; j++){ 14 if(a[i]==b[j]) 15 break; 16 } 17 if(j>=n){ 18 c[k]=a[i]; 19 k++; 20 } 21 } 22 for(i=0; i<n; i++){ 23 for(j=0; j<m; j++){ 24 if(b[i]==a[j]) 25 break; 26 } 27 if(j>=m){ 28 c[k]=b[i]; 29 k++; 30 } 31 } 32 printf("%d", c[0]); 33 for(i=1; i<k; i++){ 34 for(j=0; j<i; j++){ 35 if(c[i]==c[j]) 36 break; 37 } 38 if(j>=i) 39 printf(" %d", c[i]); 40 } 41 return 0; 42 }