这是教科书的问题。我需要让用户能够输入整数并查看最大值并计算最大数字出现的次数。除了点票之外我把一切都搞定了。我上周一直试图解决这个问题。
#include <stdio.h>
#include <limits.h>
int bigEof(void);
int main(void){
bigEof();
}
int bigEof(){
int num;
int big;
int numOld;
int count = 0;
int programFinish = 0;
big = INT_MIN;
printf("Please enter an integer: ");
while (programFinish == 0){
scanf("%d", &num);
if (num > big)
{
big = num;
}
numOld = num;
if (numOld == big){
count++;
}else
count--;
printf("Please enter next Integer <EOF> to stop: ");
printf("The current biggest number is %d and is repeated %d times.", big, count);
}
return big;
}
答案 0 :(得分:0)
可以通过以下方式定义该功能
void bigEof()
{
int num;
int big;
int count = 0;
printf("Please enter an integer: ");
while ( scanf( "%d", &num ) == 1 )
{
if ( count == 0 || big < num )
{
big = num;
count = 1;
}
else if ( big == num )
{
++count;
}
printf( "Please enter next Integer <EOF> to stop: " );
}
if ( count != 0 )
{
printf( "The current biggest number is %d and is repeated %d times\n", big, count );
}
else
{
puts( "You did not enter numbers." );
}
}
答案 1 :(得分:0)
不需要count--
,每次获得count
的新值时,您都必须重置big
值。
#include <stdio.h>
#include <limits.h>
int bigEof(void);
int main(void){
bigEof();
}
int bigEof(){
int num;
int big;
int numOld;
int count = 0;
int programFinish = 0;
big = INT_MIN;
printf("Please enter an integer: ");
while (programFinish == 0){
scanf("%d", &num);
if (num > big)
{
big = num;
count = 0;
}
numOld = num;
if (numOld == big){
count++;
}
printf("Please enter next Integer <EOF> to stop: ");
printf("The current biggest number is %d and is repeated %d times.", big, count);
}
return big;
}