C++ 输入若干个数,求最大值

输入两个数,我会;

输入固定个数的数,也会;

但是,输入不确定个数的数,输出最大的数,就不会了,还是不理解数组的基础问题,求大神给予解答::

C++ 输入若干个数,求最大值

不需要用到数组就可以:

#include <stdio.h>
#include<iostream>
using namespace std;

int main ()
{
int n;
cin>>n;
int x ,num = -1;
for (int i = 0; i < n; i++)
{
cin>>x;
if (x > num)
{
num = x;
}
}
cout<<num;
return 0;
}追问

程序有点问题:

如 输入 6 1 2 3 4 5 能正常输出 5
但是我的题目,不是这样的:
是: 输入 任意数量的数,求最大值

您老能否再给我写个

如 输入 1 2 3 4 5 6 屏幕输出 6

如 输入 1 2 3 2屏幕输出 3

追答

不好意思 ,刚看到。。。
如果是任意输入的话,需要一个函数:cin.eof()
就可以一直输入数据,如果想结束输入,ctrl+z
#include
#include
using namespace std;

int main ()
{
int x ,num = -1;
while(!cin.eof())
{
cin>>x;
if (x > num)
{
num = x;
}
}
cout<<num;
return 0;
}

温馨提示:答案为网友推荐,仅供参考
相似回答