这是一个工作代码,我只是想问一下是否可以减少 if 语句。我可以在单个 if 语句中编写所有条件吗?
我尝试使用 less if 语句。
int main()
{
int a, b, c, maximum;
printf("Enter three numbers:");
scanf("%d%d%d", &a, &b, &c);
if ((a > b) && (a > c))
{
maximum = a;
}
else if ((b > a) && (b > c))
{
maximum = b;
}
else {
maximum = c;
}
printf("The Maximum among three is = %d", maximum);
return 0;
}
最佳答案
您可以通过减少比较来简化代码:
maximum = a;
if(maximum < b) { maximum = b; }
if(maximum < c) { maximum = c; }
如果您对数组进行搜索,这与循环展开优化相同。
关于c - 有什么办法可以在不使用三元运算符的情况下减少 IF 语句吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57252079/