查找数组中出现次数最多的数字

约翰·霍尔

我已经调试了一个晚上,但是仍然不知道出了什么问题。假设我输入了6个数字组成的数组,它们分别为{100,150,150,200,200,250},因为150和200出现相同的时间,然后输出较小的数字。

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

int main() {
    int  arr[20] = { NULL }, num, result;
    printf("Input a number(1-20) and enter a series of numbers in ascending order: \n");
    scanf_s("%d", &num);
    for (int k = 0; k < num; k++) {
        scanf_s("%d", &arr[k]);
    }
    int c1, c2, i, j;
    int temp = 0;
    j = result = 0;
    c1 = c2 = 1;
    for (i = 1; i <= num-2; i++) {                /*Add c1 if the value is the same*/
        int a = arr[i];
        if (arr[i+1] == a) c1++;
        else {
            j = i + 1;
            temp = arr[j];
            while (1) {
                if (arr[j+1] == temp) {
                    c2++;
                    j++;
                }
                else break;
            }
        }
        if (c2 > c1) {                  /*Move i to the position after j*/
            c1 = 0;
            result = temp;
        }   
        if ((c2 < c1) || (c2 == c1)) {  /*Move j to the next position*/
            result = a;
            c2 = 0;
        }
        i = j + 1;
    }
    printf("Number that appears the most time:%d\n", result);
    return 0;
}

我取得一些进展后将进行每次编辑这是我到目前为止所做的。输出对于{100,150,150,200,200,250}是正确的,但是如果我输入一个包含8个数字的较大数组{100,100,100,150,150,200,250,300},现在循环将卡住。救命@@

阿特雷加拉夫

错误在这部分中:

while (i <= num) {                /*Add c1 if the value is the same*/
        if (arr[i+1] == a) 

在这里我应该小于num-1,当您得到数字时,索引从0到num-1,通常i<num可以,但是您已经i+1在循环内完成,因此循环只能从0到进行num-2虽然,从你的逻辑应该是arr[i]arr[i+1]

而这部分

        int a = arr[i];
        if (arr[i+1] == a) c1++;

为什么要给arr [i]的值赋一个值,然后再次检查arr [i + 1]?您只会使c1随着每个重复值的增加而增加。以及这部分的结尾。

i=j+1

这导致了无限循环。

尽管这不是一个好的解决方案,但这可能会解决您的问题:

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

int main() {
    int  arr[20], num, result;
    printf("Input a number(1-20) and enter a series of numbers in ascending order: \n");
    scanf("%d", &num);
    printf("Now enter the numbers");
    for (int k = 0; k < num; k++) {
        scanf("%d", &arr[k]);
    }
    int c1, c2, i, j;
    int temp = 0;
    j = 0;
    result=arr[0];
    c2 = 1;
    for (i = 0; i < num; i++) {                /*Add c1 if the value is the same*/
        int a = arr[i];
        c1=1;
        for(j=i+1;j<num;j++)
        {
            if (arr[i]==arr[j])
            {
                c1+=1;
            }
        }
        if(c1>c2)
        {
            c2=c1;
            result=arr[i];
        }
        else if(c1==c2 && result>arr[i])
        {
            result=arr[i];
        }
    }
    printf("Number that appears the most time:%d\n", result);
    return 0;
}

除此之外,它还有一个更好的方法来解决您的问题。制作一个{int key,int count}的结构,然后用它存储数组中所有唯一成员的计数,然后提取所需的内容。

这是我所说的代码,如果您还不了解指针和动态内存,可能很难看一下。但是你会明白的。

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

int main()
{
    struct counts
    {
        int key;
        int count;
    };
    int * data;
    int num;
    printf("Enter the number of data:");
    scanf("%d",&num);
    data=malloc(num*sizeof(int));

    int i,j;
    printf("Enter the data in order:");
    for (i=0;i<num;i++)
    {
        scanf("%d",data+i);
    }

    struct counts *table;
    int table_len=1;
    int flag;
    table=malloc(sizeof(struct counts));
    table[0].key=data[0];
    table[0].count=1;
    for (i=1;i<num;i++)
    {
        flag=0;
        for(j=0;j<table_len;j++)
        {
            if (table[j].key==data[i])
            {
                flag=1;
                table[j].count+=1;
                break;
            }
        }
        if (flag==0)
        {
            table=realloc(table,++table_len* sizeof(struct counts));
            table[table_len-1].key=data[i];
            table[table_len-1].count=1;
        }
    }
    //if you want to see at the table
    printf("data\t\tcount\n");
    for(i=0;i<table_len;i++)
    {
        printf(" %d\t\t%d\n",table[i].key,table[i].count);
    }
    //now to extract the value
    int answer,count;
    answer=table[0].key;
    count=table[0].count;
    for(i=1;i<table_len;i++)
    {
        if(count>table[i].count)
        {
            continue;
        }
        else if(count<table[i].count)
        {
            answer=table[i].key;
            count=table[i].count;
        }
        else if(answer>table[i].key)
        {
            answer=table[i].key;
        }
    }
    printf("The number with highest frequency is: %d\n",answer);
    return 0;    
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

查找数组中出现次数最多的相同数字

来自分类Dev

查找数组中出现次数最多的相同数字

来自分类Dev

ruby:查找数组中出现次数最多的项目,如果有

来自分类Dev

SQL查询以查找不嵌套的表中出现次数最多的值

来自分类Dev

查找字符串中出现次数最多的字符

来自分类Dev

SQL查询以查找不嵌套的表中出现次数最多的值

来自分类Dev

查找字符串中出现次数最多的字符

来自分类Dev

获取列表中出现次数最多的3个数字

来自分类Dev

获取矩阵行中出现次数最多的数字并连接结果

来自分类Dev

试图获得句子中出现次数最多的情态动词

来自分类Dev

C#:获取列表中出现次数最多的元素?

来自分类Dev

如何找到在python词典中出现次数最多的值?

来自分类Dev

如何获得集合中出现次数最多的值?

来自分类Dev

给定字符串中出现次数最多的词

来自分类Dev

查找数组中出现最多的数字 - C 问题

来自分类Dev

在“球拍”列表中查找出现次数最多的元素

来自分类Dev

查找数组中重复次数最多的算法

来自分类Dev

您如何找到MongoDB词典中出现次数最多的字段?

来自分类Dev

使用SQL搜索字符串中出现次数最多的值

来自分类Dev

如何找到列表中出现次数最多的两个字符串?

来自分类Dev

MySql:检索另一列中列中出现次数最多的值

来自分类Dev

选择在其他特定表中出现次数最多的行作为外键

来自分类Dev

Linq 分组依据并选择子组中出现次数最多的项目

来自分类Dev

检查哪个对象在列表中出现次数最多 C#

来自分类Dev

元素出现次数最多的列表

来自分类Dev

打印出现次数最多的素数

来自分类Dev

显示出现次数最多的组

来自分类Dev

查找数组中匹配次数最多(不匹配次数更少)的文档

来自分类Dev

查找哪个字符出现在字符串中的次数最多

Related 相关文章

  1. 1

    查找数组中出现次数最多的相同数字

  2. 2

    查找数组中出现次数最多的相同数字

  3. 3

    ruby:查找数组中出现次数最多的项目,如果有

  4. 4

    SQL查询以查找不嵌套的表中出现次数最多的值

  5. 5

    查找字符串中出现次数最多的字符

  6. 6

    SQL查询以查找不嵌套的表中出现次数最多的值

  7. 7

    查找字符串中出现次数最多的字符

  8. 8

    获取列表中出现次数最多的3个数字

  9. 9

    获取矩阵行中出现次数最多的数字并连接结果

  10. 10

    试图获得句子中出现次数最多的情态动词

  11. 11

    C#:获取列表中出现次数最多的元素?

  12. 12

    如何找到在python词典中出现次数最多的值?

  13. 13

    如何获得集合中出现次数最多的值?

  14. 14

    给定字符串中出现次数最多的词

  15. 15

    查找数组中出现最多的数字 - C 问题

  16. 16

    在“球拍”列表中查找出现次数最多的元素

  17. 17

    查找数组中重复次数最多的算法

  18. 18

    您如何找到MongoDB词典中出现次数最多的字段?

  19. 19

    使用SQL搜索字符串中出现次数最多的值

  20. 20

    如何找到列表中出现次数最多的两个字符串?

  21. 21

    MySql:检索另一列中列中出现次数最多的值

  22. 22

    选择在其他特定表中出现次数最多的行作为外键

  23. 23

    Linq 分组依据并选择子组中出现次数最多的项目

  24. 24

    检查哪个对象在列表中出现次数最多 C#

  25. 25

    元素出现次数最多的列表

  26. 26

    打印出现次数最多的素数

  27. 27

    显示出现次数最多的组

  28. 28

    查找数组中匹配次数最多(不匹配次数更少)的文档

  29. 29

    查找哪个字符出现在字符串中的次数最多

热门标签

归档