我想编写一个程序,如果第一个数字较大,则将第二个数字乘以 2;如果第二个数字较大,则将第一个数字除以 2。是否有其他方法可以在不使用循环或:的情况下执行此操作? ?
最佳答案
您可以使用可转换为 0
或 1
的 bool 表达式,并且当值为 0
时该表达式无效。
multiplies the second number by 2 if the first one was bigger
second = second + (first > second) * second;
and divides the first one by 2 if the second one was bigger.
first = first - (second > first) * (first / 2);
关于c - 编写不使用 IF 或 :? 条件运算符的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46753422/