c ++仅输出最大数组值和计数

时间:2015-04-05 03:21:16

标签: c++

我正在以格式arr [i]>比较数组值。 ARR [MAX]

它输出的所有值都大于arr [0]。

你们可以给我一些关于如何输出最大价值的提示吗?

代码:

    for(int i=0;i<4;i++){   
        cout<<"Enter how many pancakes eaten by the Person #"<<i<<" : ";
        cin>>pancake;
        person[i] = pancake;            
    }
    for(int i=0;i<4;i++){
        if(person[i]>person[i+1]){
            pancakeCount = pancake[i];
            cout<<"The number of most eaten pancakes is "<<pancakeCount<<" by Person#"<<i<<endl;
        }
    }

问题在于输出:

输出:

    //list of persons and the number of pancakes they ate.
    The number of most eaten pancakes is 10 by Person#7
    The number of most eaten pancakes is 10 by Person#9

每当我在最后一次迭代之前放入最大值时,它也会输出包含最大值的最后一次迭代,即Person#7

我只希望最高值成为输出本身。

3 个答案:

答案 0 :(得分:0)

int pancakeCount(0), max_person(0);
for(int i=0;i<4;i++){   
    cout<<"Enter how many pancakes eaten by the Person #"<<i<<" : ";
    cin>>pancake;
    person[i] = pancake;            
}
for(int i=0;i<4;i++){
    if(person[i]>pancakeCount){
        pancakeCount = pancake[i];
        max_person = i;
    }
}

cout<<"The number of most eaten pancakes is "<<pancakeCount<<" by Person#"<<i<<endl;

答案 1 :(得分:0)

拆分循环以找到最大值并打印最大值。

代码:

#include <iostream>
using namespace std;

int main(){
    int person[4];
    int pancake, pancakeMax, pancakeMin;
    for (int i = 0; i<4; i++)
    {
        cout << "Enter how many pancakes eaten by the Person #" << i << " : ";
        cin >> pancake;
        person[i] = pancake;
    }

    //equate to one of the elements to make sure negative numbers are accomodated
    pancakeMax = pancakeMin = pancake;

    // most eaten
    for (int i = 0; i<4; i++)
    {
        if (person[i]>pancakeMax)
        {
            pancakeMax = person[i];
        }
    }
    for (int i = 0; i<4; i++)
    {
        if (person[i] == pancakeMax)
        {
            cout << "The number of most eaten pancakes is " << pancakeMax << " by Person#" << i << endl;

        }
    }

    //least eaten
    for (int i = 0; i<4; i++)
    {
        if (person[i]<pancakeMin)
        {
            pancakeMin = person[i];
        }
    }
    for (int i = 0; i<4; i++)
    {
        if (person[i] == pancakeMin)
        {
            cout << "The number of least eaten pancakes is " << pancakeMin << " by Person#" << i << endl;

        }
    }
    return 0;
}

如果您认为逻辑只需要一个值,则可以在第二个循环中添加break语句。否则,现在,它会让所有出现的最大煎饼食用。

如果你的目标是效率,你可以像这样合并循环:

#include <iostream>
using namespace std;

int main(){
    int person[4];
    int pancake, pancakeMax, pancakeMin;
    int iMax, iMin;
    for (int i = 0; i<4; i++){
        cout << "Enter how many pancakes eaten by the Person #" << i << " : ";
        cin >> pancake;
        person[i] = pancake;
    }

    //equate to one of the elements to make sure negative numbers are accomodated
    pancakeMax = pancakeMin = pancake;

    for (int i = 0; i<4; i++){
        pancake = person[i];
        if (pancake>pancakeMax){// most eaten
            pancakeMax = pancake;
            iMax = i;
        }
        if (pancake < pancakeMin){//least eaten
            pancakeMin = pancake;
            iMin = i;
        }
    }

    cout << "The number of most eaten pancakes is " << pancakeMax << " by Person#" << iMax << endl;
    cout << "The number of least eaten pancakes is " << pancakeMin << " by Person#" << iMin << endl;

    return 0;
}

答案 2 :(得分:0)

您的代码是错误的算法尝试我的

for(int i=0;i<4;i++)
{   
  std::cout<<"Enter how many pancakes eaten by the Person #"<<i<<" : ";
  std::cin>>pancake;
  person[i] = pancake;            
}
for(int i=0;i<4;i++)
{
  if(person[i]>pancakeCount)
     {
        pancakeCount = person[i];
        personWhoEatMost = i;
     }
}
std::cout<<"The number of most eaten pancakes is "<<pancakeCount
         <<" by Person#"<<personWhoEatMost<<std::endl;