最大的价值来自煎饼的贪吃
Maximum value from pancakes glutton
我在stackoverflow上看过一些帖子。但是他们把我弄糊涂了。我无法理解背后的逻辑。所以我问这个问题:
#include <iostream>
using namespace std;
int main ()
{
int pancakes[10] = { 0};
int z = 0;
int i = 0;
int c = 1;
int temp;
do
{
cout << "Please enter the number of pancakes eaten by person " << c++ << " : ";
cin >> z;
cout << "Person " << i << " eaten " << z << " cakes in morning! " << endl;
pancakes[i] = z;
i++;
}while ( i < 10);
for ( i = 1; i < 10; i++)
{ //For loop to search person eaten maximum pancakes
if ( pancakes[i]>temp)
{
temp = pancakes[i];
cout << "The person who eaten most number is " << temp <<endl;
}
}
system("pause");
}
For循环在屏幕上打印最大值,但它也打印一些其他值。当我得到最大值时它怎么能打印出吃最多煎饼的人呢?例如,如果第6个人吃了最多的煎饼。然后它应该打印,6号人吃的煎饼最多。我正在使用Devcpp IDE。
问题:
变量名太可怕了。一个好的变量名描述了变量所代表的内容。如果不通读这段代码,看看它是如何使用的,没有人能告诉你z是什么。
temp
似乎存储了最多的煎饼,未初始化。这将导致各种古怪的行为。容易固定:
int temp = 0;
或更好
int mostPancakesEaten = 0;
最后一个for循环将在每次发现一个吃的比之前所有吃的都多的人时打印出当前吃煎饼的人。
假设三个吃煎饼的人依次吃1、2和5个煎饼
对于人1什么都没有发生!for循环从i = 1开始,person 1在i = 0;
对于person 2
if ( 2>0) // true, enter
{
temp = 2;
cout << "The person who eaten most number is " << 2 <<endl;
}
输出:吃数字最多的人是2
对于person 3
if ( 5>2) // true, enter
{
temp = 5;
cout << "The person who eaten most number is " << 5 <<endl;
}
输出:吃数字最多的人是5
注意的是煎饼的数量,而不是吃煎饼的人。
不吃东西的4到10人也被处理:
if ( 0>5) // false, skip
if ( 0>5) // false, skip
if ( 0>5) // false, skip
if ( 0>5) // false, skip
if ( 0>5) // false, skip
if ( 0>5) // false, skip
if ( 0>5) // false, skip
所以输出是
The person who eaten most number is 2
The person who eaten most number is 5
大胜。
如何修复:
都扔了。对不起。不值得修改。
#include <iostream>
#include <limits> // needed for std::numeric_limits<std::streamsize>::max()
int main ()
{
int numPancakes = 0;
int person;
int mostPancakes = 0;
int ateMost = 0;
for ( person = 1; person <= 10; person++) // for persons 1 through 10
{
std::cout << "Please enter the number of pancakes eaten by person "
<< person << " : " << std::endl;
while (!(std::cin >> numPancakes) && numPancakes < 0)
{ // user put in a bad value. Keep asking until they get it right
std::cin.clear(); // clear error flag
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n');
// Throw out any other garbage the user fed in up to the end of the line
std::cout << "Please enter the number of pancakes eaten by person "
<< person << " : " << std::endl;
}
std::cout << "Person " << person << " ate " << numPancakes
<< " cakes in the morning! " << std::endl;
if (numPancakes > mostPancakes)
{
mostPancakes = numPancakes;
ateMost = person;
}
}
std::cout << "The person who ate the most pancakes is " << ateMost << std::endl;
}
在for循环中不应该计数。for循环是用来查找吃煎饼最多的人,直到之后for循环结束,你才能知道这个人是谁。
相反,在for循环期间记录谁吃了最多的煎饼:
-
声明一个int来表示吃煎饼最多的人的索引。在 for循环之前执行
-
在for循环的if中,将int设置为等于i。
-
计算的值(以及任何其他信息,如吃的煎饼的最大数量)在 for循环结束后
编辑:将int temp改为int temp = 0
您的代码包含太多不必要的变量。此外,你的方法并不能算出谁吃了最多的煎饼,而是他们吃了多少个煎饼。
同样,您应该将任何变量的范围限制到必要的最小值(以避免错误),并为它们提供有意义的名称(而不是i
和z
等)。最后,永远不要说using namespace std
。
#include <iostream>
int main ()
{
int pancakes[10]; // the number of pancakes each person had
// 1. intialise pancakes[]
for(int person=0; person!=10; ++person) { // loop to read data from stdin
std::cout << "Please enter the number of pancakes eaten by person "
<< person++ << " : ";
std::cin >> pancakes[person];
std::cout << "Person " << person << " has eaten " << pancakes[person]
<< " cakes in morning! " << std::endl;
}
// 2. find the first person who ate most
int ate_most=0; // only need one variable: person who ate most
// initially, person 0 is our candidate
for(int person=1; person!=10; ++person) // loop to check other persons
if(pancakes[person] > pancakes[ate_most])
ate_most=person;
// 3. print out result
std::cout << "Person " << ate_most << " has eaten most pancakes, namely "
<< pancakes[ate_most] << std::endl;
}
- 没有找到相关文章