我是编程初学者,如您所见,我创建了一个程序,要求用户输入三个数字。它将显示给定数字中最大的一个。但是当我写完代码后,我想到了一个问题,如果要求用户输入数百个数字,并且应该显示给定数字中最大的一个怎么办。那么问题来了,这样做有可能吗?我需要学习什么才能产生那个结果?你有什么提示可以给我吗?
#include <iostream>
#include <string>
using std::cout, std::cin, std::endl, std::string;
int main() {
string result = " is the greatest among the numbers given";
double x, y, z;
cout<<"Enter three numbers to decide which is the largest: "<<endl;
cin >>x;
cin >>y;
cin >>z;
system("clear");
if(x>y && x>z){
cout<< x << result;
} else if (y>z && y>x){
cout << y << result;
} else
cout<< z << result;
return 0;
}
最佳答案
使用下面的程序,您可以从用户那里得到任意数量的数字,并找出其中最大的一个。
#include <iostream>
int main()
{
int size=0, largestValue=0, value=0;
std::cout << "Enter total numbers you want to add :" << "\n";
std::cin >> size;
for (int i{ 0 }; i < size; ++i)
{
std::cout << "Enter value to add : ";
std::cin >> value;
if (i == 0 || value > largestValue)
{
largestValue = value;
}
}
std::cout << "Largest value = " << largestValue << "\n";
return 0;
}
https://stackoverflow.com/questions/69193405/