给定三个数字A,B和C;任务是在三个中找到最大的数字。
例子:
Input: A = 2, B = 8, C = 1
Output: Largest number = 8
Input: A = 231, B = 4751, C = 75821
Output: Largest number = 75821
在以下程序中,找到三个数字中最大的一个, , , 和被使用。
查找三个数字中最大的算法:
1. Start
2. Read the three numbers to be compared, as A, B and C.
3. Check if A is greater than B.
3.1 If true, then check if A is greater than C.
3.1.1 If true, print 'A' as the greatest number.
3.1.2 If false, print 'C' as the greatest number.
3.2 If false, then check if B is greater than C.
3.1.1 If true, print 'B' as the greatest number.
3.1.2 If false, print 'C' as the greatest number.
4. End
找到三个数字中最大的一个流程图:
下面是在三个数字中找到最大值的C程序:
示例1:仅使用if语句查找最大数目。
#include
int main()
{
int A, B, C;
printf("Enter the numbers A, B and C: ");
scanf("%d %d %d", &A, &B, &C);
if (A >= B && A >= C)
printf("%d is the largest number.", A);
if (B >= A && B >= C)
printf("%d is the largest number.", B);
if (C >= A && C >= B)
printf("%d is the largest number.", C);
return 0;
}
输出:
Enter the numbers A, B and C: 2 8 1
8 is the largest number.
示例2:使用if-else语句查找最大数量。
#include
int main()
{
int A, B, C;
printf("Enter three numbers: ");
scanf("%d %d %d", &A, &B, &C);
if (A >= B) {
if (A >= C)
printf("%d is the largest number.", A);
else
printf("%d is the largest number.", C);
}
else {
if (B >= C)
printf("%d is the largest number.", B);
else
printf("%d is the largest number.", C);
}
return 0;
}
输出:
Enter the numbers A, B and C: 2 8 1
8 is the largest number.
示例3:使用嵌套的if-else语句查找最大数目。
#include
int main()
{
int A, B, C;
printf("Enter three numbers: ");
scanf("%d %d %d", &A, &B, &C);
if (A >= B && A >= C)
printf("%d is the largest number.", A);
else if (B >= A && B >= C)
printf("%d is the largest number.", B);
else
printf("%d is the largest number.", C);
return 0;
}
输出:
Enter the numbers A, B and C: 2 8 1
8 is the largest number.
示例4:使用三元运算符查找最大数。
#include
int main()
{
int A, B, C, largest;
printf("Enter three numbers: ");
scanf("%d %d %d", &A, &B, &C);
largest = A > B ? (A > C ? A : C) : (B > C ? B : C);
printf("%d is the largest number.", largest);
return 0;
}
输出:
Enter the numbers A, B and C: 2 8 1
8 is the largest number.
想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。