//1_16 /* Name: 1_16 Copyright: 秒大刀 Author: 秒大刀 Date: 24-02-05 22:27 Description: 试写一个算法,自大至小依次输出顺序读入的三个整数X、Y和Z的值。 Algorithm description:利用三次比较得出结果 */ struct num3 { int first,second,third; }; num3 sort3(const num3& In) { num3 Out; if(In.first>In.second) { Out.first=In.first; Out.third=In.second; } else { Out.first=In.second; Out.third=In.first; } //Until now,Out.first>Out.third if(Out.third<In.third) { Out.second=In.third; } else { Out.second=Out.third; Out.third=In.third; } //Until now,the min is in Out.third if(Out.first>Out.second) { return Out;//We have got the answer. } else {//Swap Out.first and Out.second. int temp=Out.first; Out.first=Out.second; Out.second=temp; } return Out;//At last we make it. } //The next is test code: #include<iostream> using namespace std; int main() { while(1) { cout<<"请输入三个数:"<<std::flush; num3 In; cin>>In.first>>In.second>>In.third; num3 Out=sort3(In); cout<<endl<<In.first<<','<<In.second <<','<<In.third<<"----SORT---->" <<Out.first<<','<<Out.second<<','<<Out.third<<endl<<endl; } return 0; }
评论