以递归的方式检查2个数组是否具有相同的元素

时间:2017-01-30 07:49:34

标签: c arrays function recursion

我编写了一个函数来检查2个数组是否具有相同的元素。但我想做一个递归函数来检查这个。你有什么想法吗? 例如,数组A B和输出:

答:[2 4 5 4]

B:[4 5 2 4]

out put 1

如果相同,

out put应为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 %d\n", 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 array\n");
    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:   %d\n", part1_excute(a,b,size));

}

1 个答案:

答案 0 :(得分:1)

我认为你正在寻找: -

/* 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 %c\n",*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(已经给出)

我想我已经通过了测试。请让我知道结果:):))