Experimental 7-1-4- not one-dimensional array to identify common elements of two arrays (20 points)

Given two integer array, this question is not asked to identify elements common to both.

Input formats:

Two input lines are given in two integer array, each of the first row is given a positive integer N ( ≤), followed by N integers, separated by a space therebetween.

Output formats:

Output in a row in numerical order is not given a total of two array elements, separated by a space between the numbers, but the end of the line can not be redundant space. Title ensure that there is at least one such number. Not repeat the same digital output.

Sample input:

10 3 -5 2 8 0 3 5 -15 9 100
11 6 4 8 2 6 -5 9 0 100 8 1

Sample output:

3 5 -15 6 4 1
 1 #include <stdio.h>
 2 int main()
 3 {
 4     int one[30] = { 0 };
 5     int two[30] = { 0 };
 6     int three[30] = { 12 };
 7     int N,M,i,j,h,k=0; 8 scanf("%d", &N); 9 for (i = 0; i < N; i++) { 10 scanf("%d", &one[i]); 11  } 12 scanf("%d", &M); 13 for (j = 0; j < M; j++) { 14 scanf("%d", &two[j]); 15  } 16 int cnt = 0; 17 for (i = 0; i < N; i++) { 18 cnt = 0; 19 for (j = 0; j < M; j++) { 20 if (one[i] == two[j]) { 21 cnt = 1; 22 break; 23  } 24  } 25 if (cnt == 0) { 26 for (h = 0; h < k+1; h++) { 27 if (three[h] == one[i]&&three[h]!=0) { 28 cnt = 1; 29  } 30  } 31 if (cnt == 0) { 32 three[k] = one[i]; 33 k++; 34  } 35  } 36  } 37 for (j = 0; j < M; j++) { 38 cnt = 0; 39 for (i = 0; i < N; i++) { 40 if (two[j] == one[i]) { 41 cnt = 1; 42 break; 43  } 44  } 45 if (cnt == 0) { 46 for (h = 0; h < k + 1; h++) { 47 if (three[h] == two[j]&&three[h] != 0) { 48 cnt = 1; 49  } 50  } 51 if (cnt == 0) { 52 three[k] = two[j]; 53 k++; 54  } 55  } 56  } 57 for (h = 0; h < k - 1; h++) { 58 printf("%d ", three[h]); 59  } 60 printf("%d", three[h]); 61 return 0; 62 63 }

The first output has ignored a number of 0, which is initialized to 0 and the array of conflict, improved after this line of code to solve the wrong answer caused when three [h] is equal to zero.

 

Guess you like

Origin www.cnblogs.com/nekonekomiao/p/11617486.html