且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

比较循环中的变量

更新时间:2021-09-13 22:16:31

您可以根据需要进行比较。你需要用代码重写这个,所以它是有道理的。通常,您为最大值创建一个变量,为最小值创建一个变量,如果新值更好,则替换当前值。
You can compare as many as you want. You need to reword this, with code, so it makes sense. Typically, you create a variable for the biggest and one for the smallest, and every one, you replace the current value if the new one is a better match.


#include<iostream>
#include<climits>
using namespace std;

int main()
{
 int max=INT_MIN,min=INT_MAX;
 int num;
 int counter =0;
 cout<<"Enter 10 numbers"<<endl;
 while(counter<10)
 {

     cin>>num;
     if ( max < num) max = num;
     if ( min > num) min = num;
     counter++;
 }
 cout << "min " << min << ", max " << max << endl;
 return 0;
 }