有没有更简单,更好的方法来解决此问题,因为
if else
话编写一个程序,该程序接收三个整数作为输入,并按升序输出数字。
不要使用循环/数组。
#include <stdio.h>
main(){
int no1;
int no2;
int no3;
int sto;
int hi;
int lo;
printf("Enter No. 1: ");
scanf("%d", &no1);
printf("Enter No. 2: ");
scanf("%d", &no2);
printf("Enter No. 3: ");
scanf("%d", &no3);
if (no1>no2) {
sto=no1;
lo=no2;
} else {
sto=no2;
lo=no1;
}
if (sto>no3) {
hi=sto;
if(lo>no3){
sto=lo;
lo=no3;
}else {
sto=no3;
}
}else hi=no3;
printf("LOWEST %d\n", lo);
printf("MIDDLE %d\n", sto);
printf("HIGHEST %d\n", hi);
getch();
}
if (a > c)
swap(a, c);
if (a > b)
swap(a, b);
//Now the smallest element is the 1st one. Just check the 2nd and 3rd
if (b > c)
swap(b, c);
注意:交换会更改两个变量的值。
如果您将一半代码隐藏在一个
swap()
函数后面,并且假装只有3行代码;为什么不将所有内容都隐藏在一个sort3()
函数中并假装它只是一行?如果显示该swap()
功能,则至少OP有机会实现原始代码(不带swap()
)是优越的(以提高性能)。更不用说这甚至行不通。变量a,b和c按值传递...(除非swap是宏,在这种情况下,哦,男孩)