首页 > 试题广场 >

末尾0的个数

[编程题]末尾0的个数
  • 热度指数:31108 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
输入一个正整数n,求n!(即阶乘)末尾有多少个0? 比如: n = 10; n! = 3628800,所以答案为2

输入描述:
输入为一行,n(1 ≤ n ≤ 1000)


输出描述:
输出一个整数,即题目所求
示例1

输入

10

输出

2
有0,必定是由2与5相乘得到,遍历n,计算每一个数中有多少5与2,取小的值即为解
n = int(input())
num_5_total = 0
num_2_total = 0
for i in range(1, n + 1):
    now_value = i
    while now_value % 5 == 0:
        num_5_total += 1
        now_value /= 5
    while now_value % 2 == 0:
        num_2_total += 1
        now_value /= 2
print(min(num_5_total, num_2_total))

发表于 2018-12-27 15:56:14 回复(0)
#归根结底是阶乘中因子5的个数
n = int(input())
print(n//5+n//25+n//125+n//625)

发表于 2018-08-20 09:15:14 回复(1)
def ZeroNum(n):
    count = 0
    while(n >= 5):
         count += n // 5
         n = n // 5
    return count

n = int(input())
print(ZeroNum(n))

发表于 2018-08-07 20:38:32 回复(0)
#counting the numbers of factor 5 each number has from 1 to n
n = int(input())
ans = 0
for i in range(1, n+1):
    if i % 5 == 0:
        temp_ans = 0
        temp = i
        while temp > 0 and temp % 5 == 0:
            temp_ans += 1
            temp //= 5
        ans += temp_ans
print(ans)

发表于 2018-08-01 09:47:32 回复(0)
N = input()
a = 1 b = 0 while N > 0:
    a = N*a
    N = N-1 while a%10 ==0:
    a = a//10  b = b+1 print b
发表于 2018-07-13 23:17:09 回复(0)
Python
def f(n):
    ans = 1
    for i in range(1,n+1):
        ans = ans*int(i)
    return ans
num,count = str(f(int(input()))),0
for i in num[::-1]:
    if i == '0':
        count += 1
    else:
        break
print(count)

发表于 2018-07-08 22:56:36 回复(0)
n=int(input())

fac=1
for i in range(1,n+1):
    fac*=i
fac=[x for x in '%d'%fac]
num=0
for i in fac[::-1]:
    if i!='0':
        break
    num+=1
print(num)


发表于 2018-05-28 22:15:26 回复(1)

技巧题,分开算能被5,25,125,625,,,,,5的n次方乘除的数的个数,能被5整除的贡献1个0,25贡献2个,但是25的两个有一个已经被5算了,所以依然一个。。。依次类推都是相当于贡献一个0,加起来即可。

N = int(input())
i = 1
ans = 0
while N//(5**i)>0:
    ans += N//(5**i)
    i+=1
print(ans)
发表于 2018-05-23 19:09:36 回复(0)
n=int(input())
ans=0
i=5
while i<=n:
    ans+=n//i
    i=i*5
print(ans)

发表于 2018-05-14 17:49:24 回复(0)
n = int(input())
res = 0
while n > 1:
    res += n // 5
    n //= 5
print(res)
求出[1, n]中有多少个因数是5即可
发表于 2018-04-03 13:04:17 回复(0)
import math
notzero = True
userinput = int(input())
result = math.factorial(userinput)
result = str(result)
counter = len(result)-1
numberofzeros = 0
while(notzero == True):
    if(result[counter] == '0'):
        numberofzeros +=1
        counter-=1
    else:
        notzero = False
print(numberofzeros)

发表于 2018-01-27 03:40:23 回复(0)
 
n = int(raw_input(""))
sum=0 while n!=0:
    sum+=n/5  n=n/5 print sum

发表于 2017-11-16 11:19:21 回复(0)
import math
num = str(math.factorial(int(input())))
print(len(num) - len(str(int(num[::-1]))))

发表于 2017-11-06 23:08:07 回复(0)
if __name__ == '__main__':
    n = int(input())
    n_5 = n // 5
    n_25 = n_5 // 5
    n_125 = n_25 // 5
    n_625 = n_125 // 5
    print(n_5+n_25+n_125+n_625)
发表于 2017-10-01 10:01:49 回复(0)
import sys
data=int(sys.stdin.readline().strip('\n'))
count=0
for i in range(1,data+1):
    while i%5==0:
        count+=1
        i=i/5
print(count)
发表于 2017-08-31 15:35:33 回复(0)
def checkPow(i,n):
    Pow=0
    while(i%n==0 and i>1):
        i/=n
        Pow+=1
    return Pow

n=int(input())
ten=0
two=0
five=0
for i in range(1,n+1):
    ans10=checkPow(i,10)
    ans5=checkPow(i,5)
    ans2=checkPow(i,2)
    
    ten+=ans10
    five+=(ans5-ans10)
    two+=(ans2-ans10)
print(ten+min(two,five))

发表于 2017-08-24 12:15:31 回复(0)
n=int(input(''))
dp_10=0
dp_5=0
dp_2=0
for i in range(1,n+1):
    while i%10==0:
        dp_10+=1
        i=int(i/10)
        while i%10!=0 and i/2-int(i/2)==0:
            dp_2+=1
            i=int(i/2)
        while i%10!=0 and i/5-int(i/5)==0:
            dp_5+=1
            i=int(i/5)        
    while i%10!=0 and i/2-int(i/2)==0:
        dp_2+=1
        i=int(i/2)
    while i%10!=0 and i/5-int(i/5)==0:
        dp_5+=1
        i=int(i/5)

print(dp_10+min(dp_5,dp_2))

发表于 2017-07-10 17:44:31 回复(0)