大家好,我刚刚完成了这个:
#include <iostream>
using namespace std;
int main()
{
char a, b, c, d, e, f;
char max;
cout << "enter a b c: ";
cin >> a >> b >> c >> d >> e >> f;
max = a;
if (b > max)
max = b;
if (c > max)
max = c;
if (d > max)
max = d;
if (e > max)
max = e;
if (f > max)
max = f;
cout << "max is " << max << "\n";
return 0;
}
这显然只适用于 6 个条目。我想这样做,如果您输入 2、3、4 或 5 个条目,它仍然有效!我猜我必须添加中断,只是不确定。
最佳答案
提示:您实际上不需要存储插入的每个字符。
你可以简单地用一个变量来保持实际的“当前最大值”,每次用户输入一个新数字时,你将“当前最大值”与新数字进行比较:如果当前最大值更大,你只需丢弃新输入,如果小于,则新输入变为新的最大值。
要允许用户输入他想要的字符数(直到他插入一个“特殊”字符退出),您可以使用 while
循环。
关于c++ - 几个数的最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7464511/