比较三个数大小的代码,多种方法
时间: 2023-07-05 19:36:33 浏览: 343
以下是几种比较三个数大小的代码实现方法:
方法一:使用 if-else 语句
```c
#include <stdio.h>
int main() {
int a, b, c;
printf("请输入三个整数:");
scanf("%d%d%d", &a, &b, &c);
if (a > b && a > c)
printf("%d最大\n", a);
else if (b > a && b > c)
printf("%d最大\n", b);
else
printf("%d最大\n", c);
return 0;
}
```
方法二:使用三目运算符
```c
#include <stdio.h>
int main() {
int a, b, c, max;
printf("请输入三个整数:");
scanf("%d%d%d", &a, &b, &c);
max = a > b ? a : b;
max = max > c ? max : c;
printf("%d最大\n", max);
return 0;
}
```
方法三:使用数组和循环
```c
#include <stdio.h>
int main() {
int nums[3], i, max;
printf("请输入三个整数:");
for (i = 0; i < 3; i++)
scanf("%d", &nums[i]);
max = nums[0];
for (i = 1; i < 3; i++)
if (nums[i] > max)
max = nums[i];
printf("%d最大\n", max);
return 0;
}
```
这些实现方法都可以正确地比较三个数的大小,并输出其中的最大值。
相关推荐
![text/plain](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)