找出用户输入的最大数字,并识别最大数字输入了多少次
Find the largest number input by the user and identify how many times the largest number was inputted
我有这个问题,用户必须输入一个整数列表,然后找出整数列表中最大的那个,然后计算输入最大数字的次数。
这是我的源代码:
int main()
{
int i, numberOfIntegers, listOfIntegers, largest = 0, occurrence = 0;
p("\n How many integers do you want to enter? ");
s("%i", &numberOfIntegers);
p("\n Input %i number of integers: ", numberOfIntegers);
for (i = 1; numberOfIntegers == listOfIntegers; i++)
{
s("%i", &listOfIntegers);
if (listOfIntegers > largest)
{
largest = listOfIntegers;
}
if (listOfIntegers == largest)
{
occurrence++;
}
}
p("\n \n The largest value is %i and the number of occurrence is %i", largest, occurrence);
return 0;
}
我的问题是我的程序不允许用户输入数字。而如果用户不能输入任何数字,程序就无法读取最大的数字和最大数字出现的次数。
我的程序输出如下所示:
How many integers do you want to enter? 13
Input 13 number of integers:
The largest value is 0 and the number of occurrence is 0
正确的输出应该是:
How many integers do you want to enter? 13
Input 13 number of integers: 5, 2, 15, 3, 7, 15, 8, 9, 5, 2, 15, 3, 7
The largest value is 15 and it was entered 3 times.
是循环的问题吗?
我在循环方面遇到了问题,所以如果有人能指出我的程序哪里出错了,那将非常有帮助。
一些问题:
猜猜 p/s 是 printf/scanf ?
for 循环条件,“numberOfIntegers == listOfIntegers” 事实上这个
应该是 i < numberOfIntegers.
即使是上面更正的程序也无法运行。例如,输入
1,3 ,5, 7,您的答案将计算出正确的最大值,但最大值的实例数将不正确。 (当找到新的最大值时,您需要重置发生次数)
if (listOfIntegers > largest)
{
largest = listOfIntegers; occurence=1 ;
}
else if (listOfIntegers == largest)
{
occurrence++;
}
我有这个问题,用户必须输入一个整数列表,然后找出整数列表中最大的那个,然后计算输入最大数字的次数。
这是我的源代码:
int main()
{
int i, numberOfIntegers, listOfIntegers, largest = 0, occurrence = 0;
p("\n How many integers do you want to enter? ");
s("%i", &numberOfIntegers);
p("\n Input %i number of integers: ", numberOfIntegers);
for (i = 1; numberOfIntegers == listOfIntegers; i++)
{
s("%i", &listOfIntegers);
if (listOfIntegers > largest)
{
largest = listOfIntegers;
}
if (listOfIntegers == largest)
{
occurrence++;
}
}
p("\n \n The largest value is %i and the number of occurrence is %i", largest, occurrence);
return 0;
}
我的问题是我的程序不允许用户输入数字。而如果用户不能输入任何数字,程序就无法读取最大的数字和最大数字出现的次数。
我的程序输出如下所示:
How many integers do you want to enter? 13
Input 13 number of integers:
The largest value is 0 and the number of occurrence is 0
正确的输出应该是:
How many integers do you want to enter? 13
Input 13 number of integers: 5, 2, 15, 3, 7, 15, 8, 9, 5, 2, 15, 3, 7
The largest value is 15 and it was entered 3 times.
是循环的问题吗?
我在循环方面遇到了问题,所以如果有人能指出我的程序哪里出错了,那将非常有帮助。
一些问题:
猜猜 p/s 是 printf/scanf ?
for 循环条件,“numberOfIntegers == listOfIntegers” 事实上这个 应该是 i < numberOfIntegers.
即使是上面更正的程序也无法运行。例如,输入 1,3 ,5, 7,您的答案将计算出正确的最大值,但最大值的实例数将不正确。 (当找到新的最大值时,您需要重置发生次数)
if (listOfIntegers > largest)
{
largest = listOfIntegers; occurence=1 ;
}
else if (listOfIntegers == largest)
{
occurrence++;
}