使用重载函数编程序分别把两个数和三个数从大到小排列。
#include<iostream> using namespace std; void sort( double x,double y ); void sort( double x,double y,double z ); int main() { sort( 5.6, 79 ); sort( 0.5, 30.8, 5.9 ); } void sort(double x,double y) { if ( x>y ) cout << x << '\t' << y << endl; else cout << y << '\t' << x << endl; } void sort( double x,double y,double z ) { double t; if( y<z ) { t = y; y = z; z = t; } if( x<z ) { t = x; x = z; z = t; } if( x<y ) { t = x; x = y ;y = t; } cout << x << '\t' << y << '\t' << z << '\t' << endl; }
void sort( double x,double y,double z )
{
double t;
if( y<z ) { t = y; y = z; z = t; }
if( x<z ) { t = x; x = z; z = t; }
if( x<y ) { t = x; x = y ;y = t; }
cout << x << '\t' << y << '\t' << z << '\t' << endl;
}
这道题你会答吗?花几分钟告诉大家答案吧!
扫描二维码,关注牛客网
下载牛客APP,随时随地刷题
#include<iostream> using namespace std; void sort( double x,double y ); void sort( double x,double y,double z ); int main() { sort( 5.6, 79 ); sort( 0.5, 30.8, 5.9 ); } void sort(double x,double y) { if ( x>y ) cout << x << '\t' << y << endl; else cout << y << '\t' << x << endl; }void sort( double x,double y,double z )
{
double t;
if( y<z ) { t = y; y = z; z = t; }
if( x<z ) { t = x; x = z; z = t; }
if( x<y ) { t = x; x = y ;y = t; }
cout << x << '\t' << y << '\t' << z << '\t' << endl;
}