[英]What is the usage of the = operator here?
以下是我在阶乘中查找尾随零数的解决方案,它有效,我发布它是为了了解算法的工作原理,即 n 除以 5 ^ i 的商之和,其中 i>0 .
#include <cmath>
long zeros(long n) {
long sum = 0;
for (int i = 1;; ++i) {
int m = n / pow(5, i);
if (m == 0)
break;
else sum += m;
}
return sum;
}
我看到了这个解决方案,它使我对=
运算符的使用感到困惑。 =5
在这种情况下是什么意思?
long zeros(long n) {
long result = 0;
while(n)
result += n/=5;
return result;
}
表达式result += n/=5
等价于首先将n
的值更新为n/5
,然后将result
的值更新为result + n
,其中n
现在为n/5
。
// result += n/=5 is same as doing
n = n / 5;
result = result + n;
这里的/=
类似于+=
,因为n/=5
等价于n = n/5
将值或变量分配给变量,即 m = n 表示如果 n 为 15,则 m 将是 n 的值,即 15
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.