魔法石的诱惑
高阶魔法石有一个特征,即它的重量进行阶乘运算后末尾有几个0,就拥有同等重量普通魔法石的几倍魔法力,例如5!=54321=120,而120后面包含1个零,这意味着该魔法石拥有同等重量的普通魔法石1倍的魔法力。
你的任务是找到最小自然数N,使N!在十进制下包含Q个零。
解题思路:
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
int ZeroTrail(int n) //计算n!的0的个数
{
int count=0;
while(n)
{
count+=n/5;
n/=5;
}
return count;
}
int main()
{
int q;
scanf("%d",&q);
if(!q)
{
printf("1\n");
return 0;
}
int i=4*q/5*5;
while(ZeroTrail(i)<q)
i+=5;
if(q==ZeroTrail(i))
printf("%d\n",i);
else
printf("No solution\n");
return 0;
}
PS:本博客属于中国石油大学胜利学院ACM协会所有!
BY:李雅琪