3编写C语言程序用两种不同的方法完成1、输入3个整数;2、按照从小到大的顺序排列并输出这三个数。提示:用i语句和冒泡法
方法一:使用if语句
#include <stdio.h>
int main() {
int a, b, c, temp;
printf("请输入三个整数:");
scanf("%d %d %d", &a, &b, &c);
if (a > b) {
temp = a;
a = b;
b = temp;
}
if (a > c) {
temp = a;
a = c;
c = temp;
}
if (b > c) {
temp = b;
b = c;
c = temp;
}
printf("从小到大的顺序排列为:%d %d %d\n", a, b, c);
return 0;
}
方法二:使用冒泡法
#include <stdio.h>
int main() {
int a[3], i, j, temp;
printf("请输入三个整数:");
for (i = 0; i < 3; i++) {
scanf("%d", &a[i]);
}
for (i = 0; i < 2; i++) {
for (j = 0; j < 2 - i; j++) {
if (a[j] > a[j+1]) {
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
printf("从小到大的顺序排列为:%d %d %d\n", a[0], a[1], a[2]);
return 0;
}
``
原文地址: https://www.cveoy.top/t/topic/fJ4w 著作权归作者所有。请勿转载和采集!