题目描述:编写一个C程序,运行时输入a,b,c三个值,输出其中最大者。
思路:先比较两个数的大小,然后将较大的数再与第三个数比较,最后输出最大值。
代码如下:
// #include: 头文件包含命令
// stdio.h: standard input & output 标准输入输出头文件,下面的printf输出函数需要用到
#include <stdio.h>
int main()
{
int a, b, c, t, max; // 定义a,b,c,t,max四个整形变量
printf("请输入三个整数:"); // 打印提示
scanf("%d %d %d", &a, &b, &c); // 依次输入三个整数,中间用空格隔开
if (a > b) // 如果a>b
{
t = a; // 将a的值赋给t
}
else
{
t = b; // 如果a<b,将b的值赋给t
}
if (t > c) // 如果t>c
{
max = t; // 将t的值赋给max
}
else
{
max = c; //如果t<c,将c的值赋给max
}
printf("max = %d\n", max); // 输出最大值max
return 0; // 函数返回值为0
}
运行结果: