采用传地址值的方法
采用引用类型作参数
#include <iostream>
using namespace std;
void paixu(double *,double *,double *);
int main()
{
double a,b,c;
cin>>a>>b>>c;
paixu(&a,&b,&c);
return 0;
}
void paixu(double *a,double *b,double *c)
{
double t;
if(*a>*b)
{
if(*b<*c)
{
t=*a;
*a=(*a>*c)?*a:*c;
*c=(t>*c)?*c:t;
t=*c;
*c=*b;
*b=t;
}
}
else
{
t=*a;
*a=*b;
*b=t;
if(*b<*c)
{
t=*a;
*a=(*a>*c)?*a:*c;
*c=(t>*c)?*c:t;
t=*c;
*c=*b;
*b=t;
}
}
cout<<*a<<' '<<*b<<' '<<*c;
}
采用引用类型作参数
#include <iostream>
using namespace std;
void paixu(double &,double &,double &);
int main()
{
double a,b,c;
cin>>a>>b>>c;
paixu(a,b,c);
return 0;
}
void paixu(double &a,double &b,double &c)
{
double t;
if(a>b)
{
if(b<c)
{
t=a;
a=(a>c)?a:c;
c=(t>c)?c:t;
t=c;
c=b;
b=t;
}
}
else
{
t=a;
a=b;
b=t;
if(b<c)
{
t=a;
a=(a>c)?a:c;
c=(t>c)?c:t;
t=c;
c=b;
b=t;
}
}
cout<<a<<' '<<b<<' '<<c;
}