从包含该数组所有不同元素的数组中取出多个子数组

时间:2019-08-20 11:01:30

标签: c

我的老师给了我一个作业问题,用户可以提供其大小的数组。

我们要做的是:-

  

1)从该数组中取出所有不同的元素。

     

2)将主数组中所有可能的子数组与   “独特的数组”。

     

3)告诉我们我们成功“发现”的次数   所有包含所有不同元素的子数组。

示例:-我们得到了一个数组:-[1,2,2,3,3] 不同的元素数组将是:-[1,2,3] 原始数组的所有子数组都将是:-

1) [1,2]
2) [1,2,2]
3) [1,2,2,3]
4) [1,2,2,3,3]   
5) [2,2]
6) [2,2,3]
7) [2,2,3,3]
8) [2,3]
9) [2,3,3]
10) [3,3]

此特定测试用例的答案应为2。因为只有(3)和(4)子数组包含所有不同的元素,即1,2和3。

第二个示例:-

给出数组:-[86,5,34,64,56,60,81,77,36,41]

第二个示例的答案为1;由于原始数组的所有元素都是不同的,因此只有一个解决方案应包含所有可能的不同元素,它们就是原始数组本身。

这是我为上述问题编写的代码:-

# include <stdio.h>
# include <stdlib.h>
# include <stdbool.h>

bool check(int *, int *, int, int);


int main()
{
    int number; // Variable name "number" which will specify the size of dynamically allocated array.

    printf("Enter the size of your array\n");
    scanf("%d",&number);

    int *array;
    array = (int*)calloc(number, sizeof(int)); // Dynamically allocated memory.


    int *temp_array;
    temp_array = (int*)calloc(number, sizeof(int)); // Temporary variable of original array.

    int i,j=0; // Counter variables for loop.

    printf("Enter the elements of arrays\n");

    for(i = 0; i < number; i++)
    {
        scanf("%d",(array + i)); //Main original array being filled.
    }

    for(i = 0; i < number; i++)
    {
        *(temp_array + i) = *(array + i); //Copying into temp.
    }



    for(i = 0; i < number; i++)
    {
        for( j = i + 1 ; j < number; j++)
        {
            if( *(temp_array + i ) == *(temp_array + j))
            {
                *(temp_array + j) = 0; // My way of removing those numbers which are the repeated. (Assigning them value of zero).
            }
        }
    }

    i=0;j=0;

    int sub_number = 0;

    while(i < number)
    {
        if(*(temp_array + i) != 0)
        {
            sub_number++; // Variable name "sub_number" which will specify the size of dynamically allocated array "sub_array".
        }
        i++;
    }

    int *sub_array ;
    sub_array = (int*)calloc(sub_number,sizeof(int));

    j=0;
    for(i = 0;i < number ;i++)
    {
        if( *(temp_array + i ) != 0)
        {
            *(sub_array + j) = * (temp_array + i ); //Transferring all the distinct values from temp_array to sub_array.
            j++;
        }
    }

    free(temp_array); //Freed "temp_array". No longer needed.
    temp_array = NULL;

    for(i = 0;i < sub_number; i++)
    {
        printf("%d ",*(sub_array + i)); // Desired array which only contains distinct and unique variables.
    }

    printf("\n");

    //CODE IS CORRECT TILL THE ABOVE LINE AND HAS BEEN VERIFIED.

    //Problem code starts from below.

    int ans = 0;    //Variable which shall calculate the answer.
    int k=0;        //New variable counter
    j=0;


    for(i=0; i < number; i++) //This loop will traverse variable "i" on array "array".
    {
        k = i;
        while(k < number)   //This loop will traverse variable "k" on array "array"
        {
            int *new_array;
            new_array = (int*) calloc ((k-i+1),sizeof(int));

                for(j = i; j < k; j++)  //This loop will assign the subset values of array "array" to array "new_array".
                {
                    *(new_array + (j - i)) = *(array + j);
                }
                if(check(new_array, sub_array, (k-i+1), sub_number) == true) //This will check if ALL the values in "sub_array" are present in "new_array" or not.
                {
                    ans++;
                }

            free(new_array);
            new_array = NULL;
            k++;
        }
    }

    printf("%d",ans);

    return 0;
}

bool check(int * new_array, int *sub_array, int new_number, int sub_number) //Function to check if ALL the values in "sub_array" are present in "new_array" or not.
{
    int i = 0;
    int j = 0;
    for(i = 0; i < new_number; i++)     //new_number is nothing but (k - i + 1)
    {
        if(*(new_array + i)  == *(sub_array + j))
        {
            j++;
            if(j == sub_number)
            {
                return true;
            }
            i = 0;
        }
    }
    return false;
}

现在,我的代码的问题部分(我相信)始于:-

    //CODE IS CORRECT TILL THE ABOVE LINE AND HAS BEEN VERIFIED.

    //Problem code starts from below.

    int ans = 0;    //Variable which shall calculate the answer.
    int k=0;        //New variable counter
    j=0;


    for(i=0; i < number; i++) //This loop will traverse variable "i" on array "array".
    {
        k = i;
        while(k < number)   //This loop will traverse variable "k" on array "array"
        {
            int *new_array;
            new_array = (int*) calloc ((k-i+1),sizeof(int));

                for(j = i; j < k; j++)  //This loop will assign the subset values of array "array" to array "new_array".
                {
                    *(new_array + (j - i)) = *(array + j);
                }
                if(check(new_array, sub_array, (k-i+1), sub_number) == true) //This will check if ALL the values in "sub_array" are present in "new_array" or not.
                {
                    ans++;
                }

            free(new_array);
            new_array = NULL;
            k++;
        }
    }

    printf("%d",ans);

    return 0;
}

bool check(int * new_array, int *sub_array, int new_number, int sub_number) //Function to check if ALL the values in "sub_array" are present in "new_array" or not.
{
    int i = 0;
    int j = 0;
    for(i = 0; i < new_number; i++)     //new_number is nothing but (k - i + 1)
    {
        if(*(new_array + i)  == *(sub_array + j))
        {
            j++;
            if(j == sub_number)
            {
                return true;
            }
            i = 0;
        }
    }
    return false;
}

问题是我的代码仅输出0,而没有其他输出。因此,我的boolean函数是否存在任何问题?还是我上面使用的if statement

1 个答案:

答案 0 :(得分:1)

下面的注释中指出的代码中的2个错误是:-

  

使用computed复制元素的循环从for(j = i; j < k; j++)迭代到i,包括k但不包括i,但是k旨在位于子数组内部,这可以通过元素k的更高数量以及在k-i+1上循环使用的边界来证明。

  

在检查中,k, k = i; and while(k < number)之后,程序控制流到i = 0语句的末尾,该语句递增for,因此下一个循环以i设置为i,这意味着不再使用1进行检查