题目:从键盘上输入五个整数分别存放到变量a,b,c,d,e中,将这五个数按升序排列并且输出。
思路:定义最大值,最小值函数。通过函数迭代,求出每组最小值,再通过对调法,将最小值一次置于变量a,b,c,d,e。即可
代码:
#include
using namespace std;
int min(int);
int max(int);
int main()
{
int a, b, c, d, e,m, A, B, C, D, E;
cout << “请分别输入5个整数” << endl;
cin >> a >> b >> c >> d >> e;
A = min(min(min(min(a, b), c), d), e);//确定第一位数字
if (b == A) m = a, a = b, b = m;
else if (c == A) m = a, a = c, c = m;
else if (d == A) m = a, a = d, d = m;
else if (e == A) m = a, a = e, e = m;
B = min(min(min(b, c), d), e);//确定第二位数字
if (c == B) m = b, b = c, c = m;
else if (d == B) m = b, b = d, d = m;
else if (e == B) m = b, b = e, e = m;
C = min(min(c, d), e);//确定第三位数字
if (d == C) m =c, c= d, d = m;
else if (e == C) m = c, c = e, e = m;
D = min(d, e);//确定第四位数字
E = max(d, e);//确定第五位数字
cout << “升阶排序为:” << A << B << C << D << E << endl;
return 0;
}
int min(int x, int y)
{
int f = 0;
f= (x < y) ? x : y;
return(f);
}
int max(int x, int y)
{
int g = 0;
g = (x > y) ? x : y;
return (g);
}
运行结果:
ps:本人是c++初学者,这是本人做作业时的一些思路分享,第一次发布,也希望大佬指正。