当前位置:网站首页>Find elements that are not common to two arrays

Find elements that are not common to two arrays

2022-06-22 08:10:00 Study hard 867

Given two arrays of integers , This problem requires us to find out the elements that are not in common .

Input format :

Input two arrays of integers in two lines , Give a positive integer on each line N(≤20), And then there was N It's an integer , Separated by spaces .

Output format :

In a row, output elements that are not common to two groups of numbers in the order given by the numbers , Numbers are separated by spaces , But there must be no extra space at the end of the line . The title guarantees that there is at least one such number . The same number is not output repeatedly .

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

Code :

 

#include <stdio.h>
int main(){
	int a[20],b[20],c[40],i,j,k,n,m,num=0,f=0;
	scanf("%d",&n);
	for(i=0;i<n;i++)
		scanf("%d",&a[i]);
	scanf("%d",&m);
	for(i=0;i<m;i++)
		scanf("%d",&b[i]);
	for(i=0;i<n;i++){
		for(j=0;j<m;j++)
			if(a[i]==b[j])
				break;
		if (j==m)
			c[num++]=a[i];
	}
	for(i=0;i<m;i++){
		for(j=0;j<n;j++)
			if(b[i]==a[j])
				break;
		if (j==n)
			c[num++]=b[i];
	}
	f=0;
	for(i=0;i<num;i++){	
		for(j=0;j<i;j++)
			if(c[i]==c[j]) break;
		if(j==i){
			if(f!=0) printf(" ");
			printf("%d",c[i]);
			f++;
		}
	}
	return 0;
}

原网站

版权声明
本文为[Study hard 867]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220802076693.html