请帮我写一个三个数之间找最大值的代码,且要求简洁,用c++语言
时间: 2023-05-11 13:02:56 浏览: 58
以下是一个简洁的 C 语言代码,用于找到三个数中的最大值:
```
#include <stdio.h>
int main() {
int a, b, c, max;
printf("请输入三个整数:\n");
scanf("%d %d %d", &a, &b, &c);
max = a > b ? a : b;
max = max > c ? max : c;
printf("最大值是:%d\n", max);
return 0;
}
```
相关问题
用C++写一个三个数找最大数
在 C 语言中,你可以使用 `if` 语句来找到三个数中的最大数。
例如,假设你想要找到三个数 `a`、`b` 和 `c` 中的最大数。你可以这样写:
```
if (a > b) {
if (a > c) {
printf("%d is the largest number.\n", a);
} else {
printf("%d is the largest number.\n", c);
}
} else {
if (b > c) {
printf("%d is the largest number.\n", b);
} else {
printf("%d is the largest number.\n", c);
}
}
```
这段代码会先比较 `a` 和 `b`,如果 `a` 比 `b` 大,就比较 `a` 和 `c`。如果 `a` 比 `c` 大,就输出 `a`;否则,输出 `c`。如果 `b` 比 `a` 大,就比较 `b` 和 `c`。如果 `b` 比 `c` 大,就输出 `b`;否则,输出 `c`。
这样就可以找到三个数中的最大数了。
用条件运算符实现在三个数中找出最大值和最小值的C++代码
```c
#include <stdio.h>
int main() {
int a, b, c, max, min;
printf("请输入三个整数:");
scanf("%d %d %d", &a, &b, &c);
/* 找出最大值和最小值 */
max = a > b ? (a > c ? a : c) : (b > c ? b : c);
min = a < b ? (a < c ? a : c) : (b < c ? b : c);
printf("最大值为:%d\n", max);
printf("最小值为:%d\n", min);
return 0;
}
```