2014-01-09 123 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;