运用了指针和引用的两种方法
法一:指针
#include<iostream>
using namespace std;
void order(int *x,int *y,int *z)
{
int temp;
if(*x>*y){
temp = *y;
*y = *x;
*x = temp;
}
if(*x>*z){
temp = *z;
*z = *x;
*x = temp;
}
if(*y>*z){
temp = *z;
*z = *y;
*y= temp;
}
return;
}
int main()
{
int a,b,c;
cin>>a;
cin>>b;
cin>>c;
order(&a,&b,&c);
cout<<a<<" "<<b<<" "<<c<<endl;
return 0;
}
法二:引用
#include<iostream>
using namespace std;
void order(int &x,int &y,int &z)
{
int temp;
if(x>y){
temp = y;
y = x;
x = temp;
}
if(x>z){
temp = z;
z = x;
x = temp;
}
if(y>z){
temp = z;
z = y;
y= temp;
}
return;
}
int main()
{
int a,b,c;
cin>>a;
cin>>b;
cin>>c;
order(a,b,c);
cout<<a<<" "<<b<<" "<<c<<endl;
return 0;
}
用temp做中间变量