当前位置:网站首页>Zero after factorial (C language)
Zero after factorial (C language)
2022-06-11 11:55:00 【Butayarou】
List of articles
Let's look at the description of the problem .
subject : Given an integer n , return n! The number of trailing zeros in the result .
Input :n = 3
Output :0
explain :3! = 6 , No trailing 0
Input :n = 5
Output :1
explain :5! = 120 , There is a tail 0
Title source ( Power button ): Zero after factorial
Topic analysis
Obviously , Directly calculate the factorial of an integer and then judge the trailing zero , Definitely not . Because if you are factoring a large integer , The result is very large , So that any type of variable used to store will generate overflow .
therefore , We have to Another angle To think about it . It's not hard to find out , When a positive integer is multiplied by 10 when , It will expand to the original 10 times , As a result, there is an additional trailing zero . But when calculating 5! when , Its result also has trailing zeros . So what caused it 5! The trailing zero in the result ?
Let's put 5! Let's expand the original formula of ,5! = 5 * 4 * 3 * 2 * 1 . You can find 5! contains 2 * 5, That is equivalent to 10.
In that case , Then we can put 10 To disassemble into 2 * 5, Analyze the problem from a smaller point of view .
Then enumerate the factorial of an integer , such as :
15! = 15 * 14 * 13 * 12 * 11 * 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1
15! = (3 * 5)*(2 * 7) * 13 *(2 * 6) * 11 *(2 * 5) * (3 * 3) * (2 * 4) * 7 *(2 * 3) * 5 *(2 * 2) * 3 * 2 * 1
( Calculate in advance to know ,15! The number of trailing zeros of the result of is 3)
Further discovery , Every time 5 The number appears as a factor 5, Every time 2 The number appears as a factor 2, in other words , factor 2 The number of must be no less than the factor 5 The number of . In other words , Find a factor 5, And there must be a factor 2 Pair with it . In that case , We can Put the question ( seek n! The number of trailing zeros in the result of ) Convert to seek n! There are factors 5 What is the number of .
But , wait a moment ! Think again ,n! There are factors 5 The number of only comes from every 5 The number of 1 Secondary factor 5 Do you ?
If it is to ask for 25! The number of trailing zeros in the result ? Unfold it ,25! = 25 * 24 * 23 * … * 2 * 1.
among 25 = 5 * 5, That is to say, a number of factors may contain more than one 5 , such as 125! in 125 = 5 * 5 * 5 etc. .
There are rules :
Every time 5 Number appears 1 Secondary factor 5,
Every time 25 Number appears 2 Secondary factor 5,
Every time 125 Number appears 3 Secondary factor 5,
…,
And so on .
Draw a picture and feel it intuitively :
Every time 25 Number appears 2 Time 5, in other words , Every time 5 Add once to the number 5, Every time 25 Add one more time to the number 5.
And so on .
in other words factor 5 The number of be equal to n/5 + n/25 + n/125 …
But it's still an old problem , Not even when the denominator is large , Will cause overflow problems . So it's written as
whenever n/5 after , hold n Updated once .
It can be understood as :
Code implementation
Then convert it into the following code :
int trailingZeroes(int n){
int cnt=0; // Recording factor 5 The number of
while(n>0)
{
cnt+=n/5;
n/=5; // to update n
}
return cnt;
}
Test general examples , The result is right .
Consider the special case :0! = 1 , The number of trailing zeros is 0.
Obviously, the result of the program running cnt = 0, Meet the conditions .
More articles
Adjust the array order so that the odd Numbers precede the even Numbers (C Language )
边栏推荐
- C # set or verify the format of text field in PDF
- js合并两个对象(面试题)
- Centos7.x下安装mysql8遇到的问题Couldn‘t open file /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022
- Where is it safer to open an account for soda ash futures? How is the deposit calculated?
- Web development model selection, who graduated from web development
- AGCO AI frontier promotion (6.11)
- Zhejiang University and Microsoft Asia Research Institute released a new method of video recognition, which can recognize video frame by frame without data marking, or can be used for sign language tr
- 《公司理财师专业能力》笔记
- Use cache to reduce network requests
- WordPress regenerate featured image plugin: regenerate thumbnails
猜你喜欢
![[file upload vulnerability 06] server file content detection and bypass experiment + image horse production method (based on upload-labs-14 shooting range)](/img/30/79516390c2b2b50a224eaa84a0c1c9.jpg)
[file upload vulnerability 06] server file content detection and bypass experiment + image horse production method (based on upload-labs-14 shooting range)

Node连接MySql数据库写模糊查询接口

C reads TXT file to generate word document

Zhejiang University and Microsoft Asia Research Institute released a new method of video recognition, which can recognize video frame by frame without data marking, or can be used for sign language tr

Interview experience of Xiaomi Android development post~

Intermediate web development engineer, interview questions + Notes + project practice

2022 | framework for Android interview -- Analysis of the core principles of binder, handler, WMS and AMS!

How does Sister Feng change to ice?

刷题笔记(十四)--二叉树:层序遍历和DFS,BFS

浙大联合微软亚研院发布视频识别新方法,可对视频逐帧识别且无需,数据标记,或可用于手语翻译等
随机推荐
Iframe value transfer
统计出现次数最多的前K个字符串
Runtime reconfiguration of etcd
异或的妙用(C语言)
Hang up the interviewer
C # apply many different fonts in PDF documents
Learning in Bi design 03
C reads TXT file to generate word document
Zhejiang University and Microsoft Asia Research Institute released a new method of video recognition, which can recognize video frame by frame without data marking, or can be used for sign language tr
NFT digital collection system development and construction process
在毕设中学习03
发布WordPress数据库缓存插件:DB Cache Reloaded 3.1
刷题笔记(十三)--二叉树:前中后序遍历(复习)
Golang uses XOR ^ to exchange two variables and encrypt / decrypt them
Read geo expression matrix
17.4 creating multiple threads, data sharing problem analysis and case code
什么是Gerber文件?PCB电路板Gerber文件简介
Gerber文件在PCB制造中的作用
WordPress数据库缓存插件:DB Cache Reloaded
中文输入法输入事件composition的使用