三数比大小(三种写法)
#include<stdio.h>
int main(){
int a, b, c,max_r,min_r;
printf("please enter a number to get the Max and the Min:\n");
//三元运算符做法:
scanf("%d %d %d", &a, &b, &c);
max_r = a > b ? (a > c ? a : c) : (b > c ? b : c);
min_r = a < b ? (a < c ? a : c) : (b < c ? b : c);
printf("Max is %d \n", max_r);
printf("Min is %d \n", min_r);
//逻辑运算符做法:
//求最大值
printf("please enter a number to get the Max and the Min:\n");
scanf("%d %d %d", &a, &b, &c);
if (a>b & a>c){
printf("Max is %d \n", a);
}
else if (b>a&b > c){
printf("Max is %d \n", b);
}
else{
printf("Max is %d \n", c);
}
//求最小值
if (a<b & a<c){
printf("Min is %d \n", a);
}
else if (b<a&b<c){
printf("Min is %d \n", b);
}
else{
printf("Min is %d \n", c);
}
//普通选择判断写法
//求最大值
printf("please enter a number to get the Max and the Min:\n");
scanf("%d %d %d", &a, &b, &c);
if (a > b){
if (a > c)
printf("Max is %d \n", a);
}
if (b > a){
if (b > c)
printf("Max is %d \n", b);
else
printf("Max is %d \n", c);
}
//求最小值
if (a < b){
if (a < c)
printf("Min is %d \n", a);
}
if (b < a){
if (b < c)
printf("Min is %d \n", b);
else
printf("Min is %d \n", c);
}
return 0;
}