0

我一直在尝试编写一个将输入存储到数组中然后允许我打印出来的程序。它还让我知道哪个数字最大。我想弄清楚的是如何让我的程序告诉我输入数组中最大数字的次数(出现次数)。到目前为止,这是我的代码。截至目前,这段代码输出了我输入到数组中的数字,数组中最大的元素,以及我输入的每个数字的出现(数字的出现是不正确的)。总的来说,每个数字的出现次数都是 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;





}
4

7 回答 7

1

我在您的代码中发现了一些问题。首先,第三个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];
    }
}
于 2015-02-12T09:21:45.100 回答
1

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

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

于 2015-02-12T09:22:24.670 回答
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);
于 2015-02-12T09:24:04.777 回答
0

对于您的第三个 for 循环,即您在数组中找出最大数的循环,我建议将 max 设置为 arrayNum[0],这样即使负数也可以工作。

然后,要知道有多少次出现最高数字,您需要一个count变量,count++每次数组中的某个数字等于最大值时,您就递增 ( )。为此,您需要另一个 for 循环。

祝你好运。

于 2015-02-12T09:20:32.133 回答
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++;
}
于 2015-02-12T09:30:26.540 回答
0

用下面的代码替换最后一个 for 循环

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

    }

 printf("Number %d: %d occurences\n", max,NoOfOccurances);
于 2015-02-12T09:19:24.113 回答
-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;
}
于 2018-02-28T12:42:47.187 回答