【计算n阶乘后面有多少个0】

xiaoxiao2021-02-27  173

一个数 n 的阶乘末尾有多少个 0 取决于从 1 到 n 的各个数的因子中 2 和 5 的个数, 而 2 的个数是远远多余 5 的个数的, 因此求出 5 的个数即可. 题解中给出的求解因子 5 的个数的方法是用 n 不断除以 5, 直到结果为 0, 然后把中间得到的结果累加. #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; int main() { int t; scanf("%d",&t); while(t--) { int n; scanf("%d",&n); int res=0; while(n) { res+=n/5; n/=5; } printf("%d\n",res); } return 0; }
转载请注明原文地址: https://www.6miu.com/read-13374.html

最新回复(0)