我编写了一个函数来检查 2 个数组是否具有相同的元素。 但是我想做一个递归函数来检查这个。 你对此有什么想法吗?例如,这里的数组 A B 和输出:
A :[2 4 5 4]
乙: [ 4 5 2 4]
出放 1
如果相同,则输出应为 1,如果不是,则为 0。
这是我的常规功能:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <assert.h>
int partition(int *a, int left, int right)
{
int first = left;
int pivot;
int pos;
srand((unsigned)time(NULL));
pos = rand() % (right - left + 1) + left;
swap(a + first, a + pos);
pivot = a[first];
while (left<right)
{
while (a[right] > pivot) right--;
while ((left < right) && (a[left] <= pivot)) left++;
if (left < right)
swap(a + left, a + right);
}
pos = right;
a[first] = a[pos];
a[pos] = pivot;
return pos;
}
void quick_sort(int *a, int first, int last)
{
int pos;
if (first<last)
{
pos = partition(a, first, last);
quick_sort(a, first, pos - 1);
quick_sort(a, pos + 1, last);
}
}
int *input_array_dyn(int n)
{
int i;
int *a;
a = (int *)calloc(n, sizeof(int));
assert(a);
printf("enter the array of length %dn", n);
for (i = 0;i<n;i++)
scanf_s("%d", a + i);
return a;
}
int part1_excute(int *a, int *b, int n)
{
int index;
int ans = 1;
quick_sort(a, 0, n - 1);
quick_sort(b, 0, n - 1);
for (index = 0;index<n;index++)
{
if (a[index] != b[index])
ans = 0;
}
if (ans == 0) return 0;
else return 1;
free(a);
free(b);
}
void main()
{
int *a, *b;
int ans = 1;
int size;
printf("Please input the size of arrayn");
scanf_s("%d", &size);
printf("First array:n");
a = input_array_dyn(size);
printf("Second array:n");
b = input_array_dyn(size);
printf("If the output is 1, the arrays are the same.n");
printf("If the output is 0, the arrays are the not same.n");
printf(" The ans is: %dn", part1_excute(a,b,size));
}
我想你正在寻找这个:-
/* created to help alex :)*/
#include<stdio.h>
int x[] = {'a', 'b', 'c', 'd'};
int y[] = {'d', 'a', 'c', 'b'};
/* core logic, which will iterate recursively.
to more accurately understand this logic, you should use
x[] = {'a', 'b', 'c', 'd'};
y[] = {'e', 'f', 'g', 'h'};
for your question replace x and y with
x[] = {2, 4, 5, 4};
y[] = {4, 5, 2, 4};
i am apologizing if names of variables or functions are inappropriate.
If program is not compiling there should be some syntax errors but logic is tested OK
*/
int compare(int *x, int lx, int *y, int ly, int called){
int len = 0;
if( ly == 0 || lx == 0)
return 0;
printf("comparing %c %cn",*x,*y);
if(*x == *y)
len += 1;
if(called)
len += compare(x+1,lx-1,y,ly,called-1);
len += compare(x,lx,y+1,ly-1,0);
return len;
}
/* returns true or false after calling the core logic */
int wraper_compare(int *x, int lx, int *y, int ly){
int len;
if (ly > lx){
len = compare(x,lx,y,ly,lx);
if (len == lx)
return 1;
else
return 0;
} else {
len = compare(x,lx,y,ly,ly);
if(len == ly)
return 1;
else
return 0;
}
}
int main(){
printf("%d n",wraper_compare(x,4,y,4));
return 0;
}
我使用了带有"a","b","c","d"的数组,因此您可以检查递归逻辑。如果 M 的大小为 x 且 N 的大小为 y,这将使用 MxN 迭代。
这在以下情况下有效:-1. 正常情况(即相同的数组长度,并且一个数组中没有重复的元素)2.多次重复元素。3.长度不同。(在这种情况下,如果较小是较大子集,则返回 1)
谢谢你给我这样的思考机会。寻找这种东西已经很长时间了(至少一年)。再次感谢您提出如此美丽的问题。+1(已经给出)
我想我已经通过了测试。请让我知道结果:):)