输入3个数a,b,c,按大小顺序输出。 C++编程

2024-12-14 08:27:09
推荐回答(3个)
回答1:

#include

using namespace std;

int main() {

int a,b,c,t;

cin >> a >> b >> c;

if ( a < b ) { t = a; a = b; b = t;}

if ( a < c ) { t = a; a = c; c = t;}

if ( b < c ) { t = b; b = c; c = t;}

cout << a << " " << b << " " << c << endl;

return 0;

}

回答2:

#include
using namespace std;
main()
{
int a=3,b=5,c=7,tmp=0;
if(a {
tmp=a;
a=b;
b=tmp;
}

if(a {
tmp=a;
a=c;
c=tmp;
}

if(b {
tmp=b;
b=c;
c=tmp;
}
cout< system("PAUSE");

}

回答3:

#include
using namespace std;

int main()
{
int a, b, c;
cout << "Enter three numbers:" << endl;
cin >> a >> b >> c;

if ( a < b)
{
// 两数交换
a ^= b;
b ^= a;
a ^= b;
}
if ( a < c)
{
a ^= c;
c ^= a;
a ^= c;
}
if ( b < c )
{
b ^= c;
c ^= b;
b ^= c;
}
cout << "The sorted are:" << endl;
cout << a << " "<< b << " " << c << endl;

return 0;
}