69,369
社区成员
发帖
与我相关
我的任务
分享
for(int i=0;i<sizeof(data)/sizeof(int);i++) {
for(int j=i;j<sizeof(data)/sizeof(int);j++) {
if(if(data[i]>data[j]) {
tmp = data[i];
data[i]=data[j];
data[j]=tmp;
}
}
}
这个代码因为是仓促写的,所以如有不完善或者错误地方请谅解,但算法的思路不变。。。。#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int a, b, c;
int t;
printf("请输入数字,不同数字之间用空格隔开:\n");
scanf_s("%d %d %d", &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;
}
printf("%d %d %d\n", a, b, c);
system("pause");
return 0;
}
原因是if ... else if ... else if
是只会执行一次,比如若if为真,后面的都不会执行;只要有一个为真,其他都不会执行了。将else去掉,每个都会判断