四位数排序

587 词

算法:和三位数排序相同,在三位数的基础上多一个与数字d的比较

#include<bits/stdc++.h>
using namespace std;
int main() {
int a, b, c, d;
int x;
scanf(“%d %d %d %d”, &a, &b, &c, &d);
if (a < b)
{
x = a; a = b; b = x;
}
if (a < c)
{
x = a; a = c; c = x;
}
if (a < d)
{
x = a; a = d; d = x;
}
if (b < c) {
x = b; b = c; c = x;
}
if (b < d) {
x = b; b = d; d = x;
}
if (c < d) {
x = c; c = d; d = x;
}
printf(“%d %d %d %d”, d, c, b, a);
return 0;
}

数组法

c++中有求最大值函数

#include<bits/stdc++.h>
using namespace std;
int main() {
int a[4],i;
for(i=0;i<4;i++){
cin>>a[i];
}
cout<<(*max_element(a, a+ 4));
}