在随机生成的数组中查找重复的数字

时间:2017-07-05 10:27:13

标签: c arrays random

我正在尝试制作一个非常简单的程序,用户输入有多少选项,程序会生成一个大小数组(选择+1),以便至少重复一个数字。我还打印了一个从最小到最大的生成选择的排序列表,只是为了看起来不错。在我被困的地方,我想让程序查看排序列表并找到最重复的选项并打印出该选择。这个计划背后的想法是让一个有趣的小随机发生器帮助做出决定(比如吃饭,出去等等),当选择艰难时。我还在学习基础C,所以如果解决方案是基本的,请原谅我。任何帮助表示赞赏。

int main() {
    int i;
    int j;
    int howmany;
    int swapped;
    int temp;

    printf("How many choices are there?\n");
    scanf(" %d", &howmany);

    int choices[howmany];
    srand(time(NULL));

    printf("\nRandomly Generated Choices\n");
    for (i = 0; i < howmany + 1; i++) {
        choices[i] = (rand() % howmany) + 1;
        printf(" %d ", choices[i]);
    }
    printf("\n");
    while (1) {
        swapped = 0;

        for (i = 0; i < howmany; i++) {
            if (choices[i] > choices[i + 1]) {
                temp = choices[i];
                choices[i] = choices[i + 1];
                choices[i + 1]= temp;
                swapped = 1;
            }
        }
        if (swapped == 0) {
            break;
        }
    }
    printf("\nRandomly Generated Choices - Sorted\n");
    for (i = 0; i < howmany + 1; i++) {
        printf(" %d ", choices[i]);
    }

    for (i = 0; i < howmany; i++) {
        if (choices[i] == choices[i + 1]) {

        }
    }
    printf("\n\nMost Frequent Choice\n");

    return 0;
}

4 个答案:

答案 0 :(得分:0)

你可以做这样的事情

int maximum=0, pos;
        int max(int x,int y)
        {
            if (x < y)
                return y;
            else
                return x;
        }

        int max_count(int i)
        {
            if (i == howmany)
                return 0;
            else
            {
                int count = 0;
                int j=0;
                for (j = 0; j < howmany; j++)
                    if (choices[i] == choices[j])
                        count++;
                if (count > maximum)
                {
                    maximum = count;
                    pos = i;
                }
                return max(count, max_count(i + 1));
            }
        }

最后

int count = max_count(0);
printf("\nMost frequent choice: %d, times:%d",choices[pos],count);

答案 1 :(得分:0)

下面的代码比您发布的代码稍微好一点......

此代码创建了最大存在,并且还指示此最大存在是否存在于多个案例中。

我认为有两个事实:

  • 最好使用函数qsort()来排序数组

  • 最好(恕我直言)使用函数malloc()(如下面的代码所示)来分配数组。

这里是代码:

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

    int main()
    {
        int i;
        int howmany,cnt,max,found,other;
        int swapped=0;
        int temp;
        int *choices=NULL;

    printf("How many choices are there?\n");
    scanf(" %d", &howmany);

    howmany+=1;

    choices=malloc(howmany*sizeof(*choices));
    if (choices==NULL) {
        return 1;
    }

    srand(time(NULL));

    printf("\nRandomly Generated Choices\n");
    for (i=0; i<howmany; i++){
        choices[i] = (rand()%howmany)+1;
        printf(" %d ", choices[i]);
    }
    puts("");

    do {
        swapped=0;

        for( i=0; i<howmany-1; i++){
            if (choices[i]>choices[i+1]){
                temp = choices[i];
                choices[i] = choices[i+1];
                choices[i+1]= temp;
                swapped = 1;
            }
        }

    } while(swapped);

    printf("\nRandomly Generated Choices - Sorted\n");
    for (i=0; i<howmany; i++){
        printf(" %d ", choices[i]);
    }
    puts("");

    i=0;other=0;found=0;max=0;
    do {
        cnt=1;temp=i;
        while(i<howmany-1 && choices[temp] == choices[++i]) {
            ++cnt;
        }

        if(cnt>max) {
            max=cnt;
            found=temp;
            other=0;
        } else if (cnt==max) {
            other++;
        }

        /* for debug purpose
        printf("cnt=%d max=%d other=%d i=%d choices[i]=%d found=%d choices[found]=%d\n",
               cnt,max,other,i,choices[i],found,choices[found]);
        */
    } while(i<howmany-1);

    if (other) {
        printf("\n\nThe 1st (between %d) Most Frequent Choice encountered is: %d",other+1,choices[found]);
    }else{
        printf("\n\nThe Most Frequent Choice is: %d",choices[found]);
    }
    printf(" with %d presence%s\n", max,(max>1)?"s":"");

    free(choices);

    return 0;
}

答案 2 :(得分:0)

其他答案看起来相当不错但是如果你缺乏经验并且宁愿坚持使用for循环而不使用malloc,我创建了一个简单的解决方案。

所以这就是我的看法:

int mostCommon;
int max = 0;
for (i = 0; i < (howmany + 1); i++) {
    temp = 0;
    for (j = 0; j < (howmany + 1); j++) {
        if (choices[i] == choices[j]) {
            temp++;
        }
    }
    if(temp > max) {
        max = temp;
        mostCommon = choices[i];
    }
}
printf("\n\nMost Frequent Choice: %d\n", mostCommon);

编辑:正如评论中所指出的那样,计数器上升到了多少+ 1。此外,temp在程序的早期初始化并且只是重复使用。

答案 3 :(得分:0)

这是一个非常简单有效的解决方案,可以在排序数组后找到最重复的选择:

int most_repeated = 0;
int most_count = 1;
for (i = 1; i <= howmany; i++) {
    if (choices[i] == choices[i - most_count]) {
        most_repeated = i;
        most_count++;
    }
}
printf("\nMost Frequent Choice: %d, %d times\n",
       choice[most_repeated], most_count);