c ++阶乘数中的零数 [英] c++ Number of zeros in a factorial number
本文介绍了c ++阶乘数中的零数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
它给出一个整数p。我必须找到一个数字n,其中n阶乘在末尾有p个数字为零。这里是我想的解决方案,但我不知道如果它是这个问题的解决方案。
int p;
int count5 = 0;
int i;
int copy_i;
printf(Enter p:);
scanf(%d,& p);
for(i = 1;; i ++)
{
copy_i = i;
while(copy_i / 5)
{
if(copy_i%5 == 0)
{
count5 ++;
copy_i = copy_i / 5;
}
else
{
break;
}
}
if(count5 == p)
{
printf(最小数字n是:%d。 ;
break;
}
else if(count5> p)
{
printf(No match for n!with%d zero。
break;
}
}
解决方案<
这听起来像一个Project Euler问题,所以我不会给出明确的解决方案。这里有两个提示:
- 你不必实际计算阶乘以找出结尾有多少个零。 li>
- 如果数字可被2 ^ N和5 ^ N除尽,则该数字在结尾处至少有N个零。
Possible Duplicate:
Counting trailing zeros of numbers resulted from factorial
It is given an integer "p". I have to find a number "n" for which "n factorial" has "p" numbers of zero at the end. Here is the solution i thought, but i am not sure if it is a solution for this problem. Do i have to make a function to calculate the factorial and another function to get the zeros?
int p;
int count5=0;
int i;
int copy_i;
printf("Enter p: ");
scanf("%d",&p);
for(i=1; ;i++)
{
copy_i=i;
while(copy_i/5)
{
if(copy_i%5==0)
{
count5++;
copy_i=copy_i/5;
}
else
{
break;
}
}
if(count5==p)
{
printf("The minimum number n is: %d.",i);
break;
}
else if(count5>p)
{
printf("No match for n! with %d zero.",p);
break;
}
}
解决方案
This sounds like a Project Euler problem, so I won't give an explicit solution. Here's two hints though:
- You do not have to actually calculate the factorial to find out how many zeroes it has at the end.
- If a number is divisible by 2^N and by 5^N, the number has at least N zeroes at the end.
这篇关于c ++阶乘数中的零数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文