虽然我已经为这个问题编写了代码,但我认为它不太短且合适。
#include <stdio.h>
int main()
{
float a, b, c;
printf("Enter ");
printf("\na: ");
scanf("%f", &a);
printf("b: ");
scanf("%f", &b);
printf("c: ");
scanf("%f", &c);
if (a == b && a == c) {
printf("All are equal");
}
else if (a == b & a > c) {
printf("%f is greatest", a);
}
else if (a == b & a < c) {
printf("%f is greatest",c);
}
else if (a == c & a < b) {
printf("%f is greatest", b);
}
else if (a == c & a > b) {
printf("%f is greatest", a);
}
else if (b == c & b < a) {
printf("%f is greatest", a);
}
else if (b == c & b > a) {
printf("%f is greatest", b);
}
else if (a > b && a >= c) {
printf("%f is the greatest", a);
}
else if (b > a && b > c) {
printf("%f is the greatest", b);
}
else if (c > a && c > b) {
printf("%f is the greatest", c);
}
return 0;
}
如何仅使用条件语句来缩短代码并使其更具可读性?
@klutt 关闭了这个重复的问题。
但是我查看了提供的类似问题的参考资料,并不满意。
首先,对类似问题提供的参考中出现的答案完全忽略了所有三个数字彼此相等的情况。例如,在某些答案中使用了函数或宏来确定最大值,但两者都不适合确定所有三个数字彼此相等。但在这种情况下,在原始问题中必须输出适当的消息。
指出比其他数字大的确切数字 a、b 或 c 的名称也会很有趣。例如,如果 a 等于 b 并且两者都大于 c,则在原始问题中输出数字 a。为了清楚起见,变量 a 的名称应该出现在输出中。
我可以建议以下解决方案。
#include <stdio.h>
int main( void )
{
float a, b, c;
printf( "Enter " );
printf( "\na: " );
scanf( "%f", &a );
printf( "b: " );
scanf( "%f", &b );
printf( "c: " );
scanf( "%f", &c );
if ( a == b && b == c )
{
puts( "All are equal" );
}
else if ( !( a < b ) && !( a < c ) )
{
printf( "a equal to %f is greatest.\n", a );
}
else if ( !( b < c ) )
{
printf( "b equal to %f is greatest.\n", b );
}
else
{
printf( "c equal to %f is greatest.\n", c );
}
}
当然最好添加一个检查来确保三个数字的输入是否正确。
如果您只创建一个附加函数,这会变得更简单:
float max(float x, float y) {
return x >= y ? x : y;
}
现在:
#include <stdio.h>
int main(void) {
float a, b, c;
printf("Enter\n");
printf("a: ");
scanf("%f", &a);
printf("b: ");
scanf("%f", &b);
printf("c: ");
scanf("%f", &c);
if (a == b && b == c) {
printf("All are equal\n");
}
else {
float m = max(max(a, b), c);
printf("%f is the greatest\n", m);
}
}
相当于:
else {
float t = a >= b ? a : b;
float m = t >= c ? t : c;
printf("%f is the greatest\n", m);
}
或者避免使用三元表达式:
else {
float t;
float m;
if (a >= b) t = a;
else t = b;
if (t >= c) m = t;
else m = c;
printf("%f is the greatest\n", m);
}
给定 3 个数字并尝试找到:
如果全部相等,则报告全部相等。
否则打印最大值。
如何缩短代码...?
OP 的方法,在最坏的情况下,确实有很多比较。即使其他答案也最多执行 4 次比较..
我断言,
a,b,c
之间最多需要3次比较(假设没有一个是NAN)。
if (a == b) {
if (b == c) printf("All are equal");
else if (a > c) printf("%f is greatest", a);
else printf("%f is greatest", c);
} else {
if (a > b) {
if (a > c) printf("%f is greatest", a);
else printf("%f is greatest", c);
else {
if (b > c) printf("%f is greatest", b);
else printf("%f is greatest", c);
}
}
使用“if”语句查找三个数字中最大的 C 程序
#include <stdio.h>
int main() {
// Declare variables to hold three numbers and the largest number
int a, b, c, largest;
// Ask the user to enter three numbers
printf("Enter three numbers: ");
// Read the three numbers entered by the user and store them in variables a, b, and c
scanf("%d %d %d", &a, &b, &c);
// Assume the first number (a) is the largest initially
largest = a;
// Compare the second number (b) with the largest so far
// If b is greater than the current largest, update largest to b
if(b > largest)
largest = b;
// Compare the third number (c) with the largest so far
// If c is greater than the current largest, update largest to c
if(c > largest)
largest = c;
// Print the largest number found
printf("Largest number is: %d\n", largest);
// Return 0 to indicate successful completion of the program
return 0;
}
如果您想编写一个程序来有效地找到三个数字中最大的一个?在我的博客上查看此综合指南:C 程序查找三个数字中的最大者