c++ - 如何在给定输入的数字中找到最大的数字?

我是编程初学者,如您所见,我创建了一个程序,要求用户输入三个数字。它将显示给定数字中最大的一个。但是当我写完代码后,我想到了一个问题,如果要求用户输入数百个数字,并且应该显示给定数字中最大的一个怎么办。那么问题来了,这样做有可能吗?我需要学习什么才能产生那个结果?你有什么提示可以给我吗?

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

相关文章:

javafx - 在 javafx 中显示单 Pane 警报

r - 如何在 R 中为函数添加红线

javascript - 如何给一个空的 td 上课?

html - 使 flexbox 垂直增长

r - 根据R中的组和行计算差异

java - 在 Windows 上使用 Jar 文件启动 JMeter 时出现白色空白屏幕

c++ - std::transform 中未解析的重载函数类型

c++ - 如何获取属于某个范围的 vector 中的最大元素?

python - 在 PySpark 中的 groupby 之后计算 sum 和 countDist

javascript - Material ui 版本 4-11.4 的文档丢失