给定若干个整数,求其中的最大值及其个数。
题目描述给定若干个整数,求其中的最大值及其个数。
输入
输入若干个int范围内的整数。
输出
输出如下:
maximum data is #, whose occurences is ?.
其中#表示最大值,?表示最大值出现的次数。
样例输入
10 3 10 3
样例输出
maximum data is 10, whose occurences is 2.
程序代码:#include <stdio.h>
#include <limits.h>
int main( void )
{
int maxval = INT_MIN;
unsigned count = 0;
for( int v; scanf("%d",&v)==1; )
{
if( v > maxval )
{
maxval = v;
count = 1;
}
else if( v == maxval )
{
++count;
}
}
printf( "maximum data is 10, whose occurences is 2.", maxval, count ); // 要不要加\n题目中没讲
return 0;
}
程序代码:#include<stdio.h>
int main()
{
int k = 0,max = 0,count = 0;
char p;
while(1)
{
scanf("%d", &k);
if (k > max)
{
max = k;
count = 1;
}
else if (k == max)
{
count++;
}
p = getchar();
if (p == '\n')
{
break;
}
}
printf("maximum data is %d, whose occurences is %d\n", max, count);
}