gpt4 book ai didi

C++数组初学者

转载 作者:行者123 更新时间:2023-11-30 03:52:12 27 4
gpt4 key购买 nike

我有这段代码,我一直在为乐趣而努力,它是最基本的,因为我是初学者,它工作正常,但我似乎无法弄清楚如何制作它显示吃的煎饼最少和最多。非常感谢您。

#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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com