2014-01-09 46 views
-4

该指数是我需要解决的方案:返回值最大

编写一个程序,要求用户输入由10个不同的人(Person 1, Person 2, ..., Person 10)吃过早餐薄煎饼的数量。
一旦输入数据,程序必须分析数据并输出哪个人吃了早餐最多的煎饼。

任何人可以写返回值最大的指数的代码(我已经标记为 “H”)

#include "iostream" 

using namespace std; 

int main() 
{ 

    int x[11]; 
    int y; 
    int h; 
    for (int i = 1; i <= 10; i++) 
    { 
     cin >> i[x]; 
     cout << "Person: " << i << " has eaten " << i[x] << " pancakes" << endl 
     y = x[0]; 
     h = x[0]; 


     for (int j = 1; j <= 10; j++) 
     { 
      if (x[j] > y) 
      { 
       y = x[j]; 
      } 
     } 
    } 
    cout << "The most pancakes are eaten by Person " << h << " with " << y << endl; 
    system("pause"); 
    return 0; 
} 
+3

_'Could anybody write the code for returns the index of maximum value'_ Certainly no!这是**你的**作业! –

+0

这个问题有什么困难?你应该能够在整个互联网上找到资源。 – bblincoe

+0

如何首先使用比x,y和h更具描述性的变量重新标记变量?可以使你的逻辑树更容易工作 – tinkertime

回答

0

没有测试,应该工作:

#include <iostream> 

using namespace std; 

int main() 
{ 

    int x[11]; 
    int ans, ansmax = 0; 
    for (int i = 1; i <= 10; i++) 
    { 
     cin >> x[i]; // You had wrong variable here 
     cout << "Person: " << i << " has eaten " << x[i] << " pancakes" << endl 
     if(x[i] > ansmax) 
     { 
      ansmax = x[i]; 
      ans = i; 
     } 
    } 
    cout << "The most pancakes are eaten by Person " << ans << " with " << ansmax << endl; 
    system("pause"); 
    return 0; 
} 
0

我的五美分

#include <iostream> 
#include <cstdlib> 

using namespace std; 

int main() 
{ 
    const int N = 10; 
    int person[N]; 

    // Entering initial data 
    for (int i = 0; i < N; i++) 
    { 
     cout << "Enter number of pancakes eaten by person " << i + 1 << ": "; 
     cin >> person[i]; 
    } 

    // Finding the index (favorite) of the person who has eaten the most pancakes. 
    int favorite = 0; 

    for (int i = 1; i < N; i++) 
    { 
     if (person[favorite] < person[i]) favorite = i; 
    } 

    // Now all is ready to show the result 
    cout << "\nThe most pancakes are eaten by Person " << favorite + 1 
     << " with " << person[favorite] << endl; 

    system("pause"); 

    return 0; 
} 
0
#include <algorithm> 

int index = std::max_element(x, x + 11) - x;