#include <iostream>
using namespace std;
int main()
{
int t;
int temp[99];
for (int i = 0; i < 10; i++) {
cin >> temp[i];
}
for (int a = 0; a < 11; a++) {
for (int b = 0; b < 11; b++) {
if (temp[a] > temp[b]) {
t = temp[a];
}
}
}
for (int a = 1; a < 11; a++) {
if (temp[a] = t) {
cout << "Person " << temp[a] << " ate the most pancakes\n" ;
}
}
system("pause>nul");
return 0;
}所以我在cplusplus.com上做这个练习问题,叫做煎饼,glutton.With,这段代码,我只是想找出谁吃了最多的煎饼,但是每次我完成这个程序,我就会得到大量的数字,重复5次。我做错什么了?这就是了。Pancake Glutton要求:变量、数据类型和数值运算符--基本输入/输出逻辑(if语句,开关语句)循环(for,while,do-while)数组。
编写一个程序,要求用户输入10个不同的人( 1人、2人、.人、10人)早餐吃薄煎饼的数量,一旦输入数据,程序必须分析数据,并输出早餐吃得最多的人的煎饼。
★修改程序,使它还输出谁吃的煎饼数量最少的早餐。
★★★★对程序进行修改,使其按10人吃薄煎饼的数量顺序输出一个列表。即
Person 4: ate 10 pancakes
Person 3: ate 7 pancakes
Person 8: ate 4 pancakes
...
Person 5: ate 0 pancakes"发布于 2013-12-30 09:36:18
您的代码包含几个bug。1)。变量t未初始化。2)。您只输入了10个元素。这意味着有效的指示符为0- 9。但是,您尝试访问11个元素,而不是10个。
for(int a = 0;a<11;a++)是不正确的。
( 3)这些循环没有任何意义。
for(int a = 0;a<11;a++)
{
for(int b = 0;b<11;b++)
{
if(temp[a]>temp[b])
{
t = temp[a];
}
}
}4)这里使用赋值运算符,而不是比较运算式。
if (temp[a] = t)5)您应该包括报头<cstdlib>,因为您使用的是在标头中声明的函数系统。
正如我所理解的,您所需要的是编写在数组元素之间找到最大值的代码。
若要查找最大元素的索引,可以使用以下代码。
int theBiggest = 0;
for ( int i = 1; i < 10; i++ )
{
if ( temp[theBiggest] < temp[i] ) theBiggest = i;
}
cout << "Person " << theBiggest << " ate the most pancakes equal to " << temp[theBiggest] << endl ;发布于 2013-12-30 09:20:43
您正在执行赋值而不是等式测试(operator=而不是operator==):
if (temp[a] == t)https://stackoverflow.com/questions/20836598
复制相似问题