当前位置:网站首页>Exercise 7-4 find out the elements that are not common to two arrays (20 points)

Exercise 7-4 find out the elements that are not common to two arrays (20 points)

2022-07-04 09:47:00 skeet follower

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

The code is as follows :

#include<stdio.h>
int main()
{
    int n1,n2,flag=0,i,j,m=0;
    int a[21],b[21],c[30];
    scanf("%d",&n1);
    for(i=0;i<n1;i++){
        scanf("%d",&a[i]);
    }
    scanf("%d",&n2);
    for(i=0;i<n2;i++){
        scanf("%d",&b[i]);
    }
    for(i=0;i<n1;i++){
        for(j=0;j<n2;j++){
            if(a[i]==b[j]){
               flag++;
                break;
            }
        }
        if(flag==0){
           c[m]=a[i];
            m++;
        }
        flag=0;
    }
    for(i=0;i<n2;i++){
        for(j=0;j<n1;j++){
            if(b[i]==a[j]){
                flag++;
                break;
            }
        }
        if(flag==0){
            c[m]=b[i];
            m++;
        }
        flag=0;
    }
    printf("%d",c[0]);
    for(i=1;i<m;i++){
        for(j=0;j<m;j++){
            if(c[i]==c[j]){
                break;
            }
        }
        if(i==j){
            printf(" %d",c[i]);
        }
    }
    return 0;
}

原网站

版权声明
本文为[skeet follower]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202141423487179.html

随机推荐