输入A,B,C三个数, 按从大到小的顺序输出,要求两个数的交换用swap函数来实现。(参

如题所述

//c++实现
//其中swap函数是属于 algorithm的库函数 void swap(T& x, T& y);
#include <iostream>
#include <algorithm>
using namespace std;
void Sort(int &x,int &y,int &z);

void main( void )
{
int a,b,c;
cout<<"Please input the three Num one by one:"<<endl;
cin>>a>>b>>c;
Sort(a,b,c);
cout<<a<<"-"<<b<<"-"<<c<<endl;
}
void Sort(int &x,int &y,int &z)
{
if(x<y)swap(x,y);
//x>y 不变
if(x<z)swap(x,z);
//x>z顺序不变
if(y<z)swap(y,z);
//y>z顺序不变
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-02-25
#include<stdio.h>
void swap(double *a,double *b);
int main()
{
double a,b,c;
scanf("%lf%lf%lf",&a,&b,&c);
if(a<b)
swap(&a,&b);
if(a<c)
swap(&a,&c);
if(b<c)
swap(&a,&c);
printf("%f,%f,%f\n",a,b,c);
return 0;
}
void swap(double *a,double *b)
{
double s;
*a=*b;
s=*a;
*b=s;
}本回答被提问者采纳
相似回答