C++ 数组初学者

标签 c++ arrays

我有这段代码,我一直在为了好玩而工作,它是最基本的,因为我是初学者,它工作得很好,但我似乎无法弄清楚如何制作它显示了吃煎饼的最少和最多的量。提前非常感谢您。

#include <iostream>
using namespace std;

int main(){

   int pancakes[10];
   int x,i;

    cout << "Hello user!" << endl;
    cout << endl;
    cout << "Please enter how many pancakes did each of the 10 people eat:" << endl;
    cout << endl;

    for (i=0;i<10;i++ ){
        cin >> x;
        pancakes[i]=x;
    }

    cout  << "1st person ate" << "  " << pancakes[0] << " " << "pancakes" << endl;
    cout  << "2nd person ate" << "  " << pancakes[1] << " " << "pancakes" << endl;
    cout  << "3rd person ate" << "  " << pancakes[2] << " " << "pancakes" << endl;
    cout  << "4th person ate" << "  " << pancakes[3] << " " << "pancakes" << endl;
    cout  << "5th person ate" << "  " << pancakes[4] << " " << "pancakes" << endl;
    cout  << "6th person ate" << "  " << pancakes[5] << " " << "pancakes" << endl;
    cout  << "7th person ate" << "  " << pancakes[6] << " " << "pancakes" << endl;
    cout  << "8th person ate" << "  " << pancakes[7] << " " << "pancakes" << endl;
    cout  << "9th person ate" << "  " << pancakes[8] << " " << "pancakes" << endl;
    cout  << "10th person ate" << " " << pancakes[9] << " " << "pancakes" << endl;

    return 0;
}

最佳答案

由于您是初学者,我将使用循环提出一个简单的解决方案。

int max = 0;
for(i = 0; i < 10; i++) {
    if(pancakes[i] > max) max = pancakes[i];
}
cout << "Most amount of pancakes eaten by a single person: " << max << endl;

关于C++ 数组初学者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30863992/

相关文章:

c++ - 有没有我们*需要*在 C++ 中 protected 继承的例子?

c++ - 如何使用 MAPI 发送邮件?

java.lang.ClassCastException : [Ljava. lang.Object;不能转换为 [Ljava.lang.String;

javascript - 如何使用Javascript计算数组中的相邻数字?

php - 在 PHP 中将一些新数据添加到数组中

c++ - 从一组非连续的连续二维切片创建一个 3d numpy 数组

c++ - 为什么 std::(i)ostream 将有符号/无符号字符视为文本而不是整数?

c++ - 将 DBL_MAX 转换为 int 的结果不同于 std::numeric_limits<double>::max() 到 int

c - 为什么所有的数组元素都被初始化为 0 即使我们几乎不给前几个元素赋值

javascript - 将对象树存储到 csv/xlsx