2015-05-17 94 views
0

1)写一个程序,要求用户输入由10个不同的人(人1,人2,...,人10)早餐吃的煎饼数量。 一旦数据输入完毕,程序必须分析数据并输出哪个人吃了早餐最多的煎饼。为什么这只适用于休息;?

我的解决方案使用数组,但程序只显示吃了最多句子的人,如果我在if语句的末尾放置一个break。如果没有休息,该计划就会问每个人吃了多少东西然后退出。我只是想明白为什么休息时间需要在那里,或者是否有办法在没有休息的情况下进行。

这里是我的代码:

//Pancakes! 
#include <iostream> 
#include <limits> 
using namespace std; 


int main() 
{ 
    //Build array of people and set a value for most eaten 
    int people[9] = {}; 
    int most = -1; 


    for (int n=1; n<=10; n++) 
    { 
     //Sets the number of pancakes eaten to a person value in the array 
     cout << "How many pancakes did person " << n << " eat? "; 
     cin >> people[n-1]; 

     //Checks if the value entered above is the highest value 
     if(people[n-1] > most) 
     { 
      most = people[n-1]; 
     } 
    } 

    //Line entered for formatting 
    cout << endl; 

    //Lists the person and how many pancakes they ate 
    for (int x=0; x<10; x++) 
    { 
     if(people[x] == most) 
     { 
      cout << "Person " << (x+1) << " ate " << most << " pancake(s), the most!" << endl; 
      break; 
     } 
    } 




    //Pause after program 
    cout << endl; 
    std::cout << "Press ENTER to continue..."; 
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); 

    return 0; 
} 

,也欢迎来审查我的代码,并给我提示使其更加简洁,因为即时通讯仍然是一个新手=]感谢。

+0

另外,只是一个即时通讯即时编写此代码在工作中,我不能安装一个编译器,所以即时在线编译[这里](http://www.onlinecompiler.net/),它不允许使用'\ ñ'这就是为什么有一堆endl大声笑 – Brian

回答

0

针对以上问题,我会解决这种方式来预览谁吃最多煎饼的人:

代码:

 // assuming the first one is the highest one 
    most = 0; 
    //now checking for the higest one 

    for (int x = 0; x < 10; x++) { 
     if (people[x] > =people[most]) { 
     most = x; 
     } 
    } 

    cout << people[most]; //this shows the highest pancakes. 

这完全不使用break,并给予必要的输出。

+0

嗯,这是非常好的感谢。我想我也可以做一个函数,并在最后调用该函数。谢谢您的帮助! – Brian

相关问题