我试图制作一个程序来检查一个数组是否包含另一个数组中的所有数组值。因此,如果这是真的,程序将使用它的值返回 1。如果不正确,它将返回 0(我将其命名为 p)。不过我没能做那个程序。请你帮助我好吗?
问问题
3112 次
1 回答
1
#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
于 2013-09-16T16:06:47.883 回答