问题描述
- 四个数排序,这个程序有什么问题?
- #include
using namespace std;
void swap(int& a int& b);
int main()
{
int a b c d;
cin >> a >> b >> c >> d;
if (b < a) swap(b a);
if (c < b) swap(c b);
if (d < c) swap(d c);
if (b < a) swap(b a);
if (c < b) swap(c b);
if (b < a) swap(b a);
cout << a << b << c << d;
}void swap(int& a int& b)
{
int c = a;
a = b;
b = c;
}
解决方案
这就是冒泡排序嘛,当然这样写有点傻,可以用循环实现。
解决方案二:
没啥,先说能遇到了什么问题
解决方案三:
如果你们老师没有限定非得用数组或者循环的话,应该没问题。
时间: 2024-08-12 19:26:14