计算最大数字的出现次数

问题描述 投票:0回答:7

我一直在尝试编写一个将输入存储到数组中的程序,然后允许我将其打印出来。它还让我知道哪个数字是最大的。我想弄清楚的是如何让我的程序告诉我数组中输入的最大数量的次数(出现次数)。到目前为止,这是我的代码。截至目前,此代码输出我输入数组的数字,数组中最大的元素,以及我输入的每个数字的出现(数字的出现不正确)。总而言之,每个数字的出现次数都为0.这显然是不正确的。同样,我需要我的程序显示最大的数字(它所做的)和仅出现的最大数字。欢迎提出所有建议,提示或想法。谢谢。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>



int main()
{
int arrayNum[15];
int a;
int max=0;
int location;


for( a=0; a < 15; a++)
    {   
        printf("Enter element %d:", a);
        scanf("%d",&arrayNum[a]);
    }

for(a=0; a < 15; a++)
    {
        printf("%d\n", arrayNum[a]);
    }

for (a = 1; a < 15; a++)
  {
    if (arrayNum[a] > max)
    {
       max  = arrayNum[a];
       location = a+1;
    }
    }
printf("Max element in the array in the location %d and its value %d\n", location, max);

for(a=0; a<15; a++)
    {
        if(arrayNum[a+1] == arrayNum[a])
            continue;
        else
            printf("Number %d: %d occurences\n", arrayNum[a]);
    }
return 0;





}
c arrays find-occurrences
7个回答
1
投票

就在你开始下面的循环之前,max仍然是0 make

  max = a[0];

  for (a = 1; a < 15; a++)
  {
    if (arrayNum[a] > max)
    {
       max  = arrayNum[a];
       location = a+1;
    }
  }

后来

int n=0;
for(i=0;i<15;i++)
{
   if(max == a[i])
   n++;
}

printf("Number of times max appears in the array is %d\n",n);

0
投票

用以下代码替换last for循环

NoOfOccurances = 0;
for(a=0; a<15; a++)
    {
        if(max == arrayNum[a])
        {
             NoOfOccurances++;
        }  

    }

 printf("Number %d: %d occurences\n", max,NoOfOccurances);

0
投票

对于你的第三个for循环,你找到你的数组中最大数字的那个,我建议将max设置为arrayNum [0],这样它甚至可以使用负数。

然后,要知道有多少次出现的最高数,你需要一个count变量,每当数组的数量等于max时你递增(count++)。要做到这一点,你需要另一个for循环。

祝好运。


0
投票

我在你的代码中发现了一些问题。首先,第三个for循环从1开始,但它不会将max更新为arrayNum[0]的值。

那么,对于手头的问题,我会有两个变量:

int max; // The maximum value
int max_count; // The count of the maximum value

然后,找到最大值和计数的逻辑如下:

对于每个元素,将其与最大值进行比较。如果相等,则增加max_count。如果它更大,请用值更新max,并将max_count设置为1.如果它更小,则忽略它。就像是:

max = arrayNum[0];
max_count = 1;
for (int a = 1; a < 15; ++a)
{
    if (arrayNum[a] == max)
       max_count++;
    else if (arrayNum[a] > max)
    {
        max_count = 1;
        max = arrayNum[a];
    }
}

0
投票

您需要做的就是引入一个新变量来跟踪max的出现次数。找到max的新值时,将该计数设置为零。当发现后续值等于max时,递增计数器。

顺便提一下,您的代码无法正确找到当前形式的最大值。尝试一个测试用例,其中数组元素都是负数。尝试另一个测试用例,其中所有值都是正数,输入的第一个值(arrayNum[0])是最大值。在这两种情况下,您会发现您的函数实际上找不到最大值。


0
投票

您可以在一个循环迭代中执行您想要的操作:

int count = 1;
int position = 0;
int max = arrayNum[0];
int N = 15;
int p;

for (p = 1; p < N; ++p)
{
    if (arrayNum[p] > max) // Find a bigger number
    {
       max  = arrayNum[p]; 
       pos = p;
       count = 1;
    }
    else if ( arrayNum[p] == max) // Another occurrences of the same number
          count++;
}

-1
投票

时间复杂度为O(n)的简单解决方案

int maxoccurence(int a[],int ar_size)
{
    int max=a[0],count=0,i;

    for(i=0;i<ar_size;i++)
    {
        if(a[i]==max)//counting the occurrence of maximum element 

        count++;

        if(a[i]>max)//finding maximum number
        {
            max=a[i];

            count=1;
        }
    }

    printf("Maximum element in the array is %d\n",max);

    return count;
}
© www.soinside.com 2019 - 2024. All rights reserved.