简体   繁体   English

= 运算符在这里的用途是什么?

[英]What is the usage of the = operator here?

Below is my solution for finding the number of trailing zeros in a factorial, it works, i'm posting it to give an idea of how the algorithm works which is the sum of quotients of n divided by 5 ^ i, where i>0.以下是我在阶乘中查找尾随零数的解决方案,它有效,我发布它是为了了解算法的工作原理,即 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;
}

I saw this solution which confused me with its use of the = operator.我看到了这个解决方案,它使我对=运算符的使用感到困惑。 What does =5 mean in this context? =5在这种情况下是什么意思?

long zeros(long n) {
    long result = 0;
    while(n)
        result += n/=5;
    return result;
}

The expression result += n/=5 is equivalent to first updating the value of n to n/5 and then updating the value of result to result + n where n is now n/5 .表达式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;

Here the /= is analogous to += in the sense that n/=5 is equivalent to n = n/5这里的/=类似于+= ,因为n/=5等价于n = n/5

将值或变量分配给变量,即 m = n 表示如果 n 为 15,则 m 将是 n 的值,即 15

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM