請看看這個簡單的C程序
vector<int> num;
int temp=0;
int max=0,min=0;
while(cin>>temp)
{num.push_back (temp);
sort(num.begin (),num.end ());
max=num[num.size ()-1];
min=num[0];
cout<<"the temp is:"<<temp<<endl;
cout<<"the min is:"<<min<<endl;
cout<<"the max is:"<<max<<endl;
if(temp<min)
cout<<"the samlest so far:"<<temp<<'\n';
else if(temp>max)
cout<<"the largest so far:"<<temp<<'\n';
}
如上:程序的意思很明了,前面temp、min和max的值都是對的。但是程序始終都不進入if分支,不知這是什么原因?求解。
[解决办法]
1、temp==min Or temp==max
即使为中间值 temp不会小于min;temp也不会大于max
[解决办法]