簡體   English   中英

檢查數組是否包含C中另一個數組的所有數組值

[英]check if array contains all array values from another array in C

我試圖制作一個程序,該程序將檢查一個數組是否包含另一個數組的所有數組值。 因此,如果是這樣,程序將使用其值返回1。 如果不正確,它將返回0(我將其命名為p)。 我沒做那個程序。 請你幫助我好嗎?

#include <stdio.h>

int isSubset(int arr1[], int arr2[], int m, int n)
{
    int i = 0;
    int j = 0;
    for (i=0; i<n; i++)
    {
        for (j = 0; j<m; j++)
        {
           if(arr2[i] == arr1[j])
              break;
        }

        /* If the above inner loop was not broken at all then
           arr2[i] is not present in arr1[] */
        if (j == m)
           return 0;
    }

    /* If we reach here then all elements of arr2[] 
      are present in arr1[] */
    return 1;
}

int main()
{
    int arr1[] = {11, 1, 13, 21, 3, 7};
    int arr2[] = {11, 2, 7, 1};

    int m = sizeof(arr1)/sizeof(arr1[0]);
    int n = sizeof(arr2)/sizeof(arr2[0]);

    if(isSubset(arr1, arr2, m, n))
      printf("arr2[] is subset of arr1[] ");
    else
      printf("arr2[] is not a subset of arr1[]");      

    getchar();
    return 0;
}



Ideone鏈接並運行http : //ideone.com/4u9oQm

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM