C++题目:任意输入n个整数,输出其中的最大值和最小值(要求不能使用数组,用for循环来解决问题)

如题所述

/*
  author: qcq
  date: 2015/3/31
  e-mail: [email protected]
 */
#include<iostream>
#include<limits>
using namespace std;
int main()
{
int temp = 0;
/*
        The method used here is below.
        What we need is three variables, one for temp variable, one for max_number, one
        for small number. Then scanning the input. what initial is to set max to the minimal number.
        min number to max number.
*/
int big = INT_MIN;
int small = INT_MAX;
/*
        when you want terminate the input, so you can input Crtl + D or Ctrl + Z.
        Good luck to you.
*/
for (;cin>>temp;){
        if (big < temp){
            big = temp;
        }
        if (small > temp){
            small = temp;
        }
}
cout<<big<<endl<<small<<endl;
    return 0;
}

追问

大哥 我运行后输入数 没出现运行结果

追答

输入完成之后输入Ctrl+D或者Ctrl+Z然后回车,你是按照我说的这样做的么?

追问

厉害 666666666666

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-03-31
用链表试试。不定长的。for中比较最大最小值就ok。追问

我是初学者 希望能讲的清楚点 谢谢啦

追答

下面那个代码不错。。。比我这个方法强。

相似回答