日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

求阶乘的第一个非零数字_查找数字阶乘中的尾随零

發(fā)布時(shí)間:2025/3/11 编程问答 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 求阶乘的第一个非零数字_查找数字阶乘中的尾随零 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

求階乘的第一個(gè)非零數(shù)字

Problem statement:

問題陳述:

Find the number of trailing zeros in n! (Where, n is the given input).

在n中找到尾隨零的數(shù)目! (其中, n是給定的輸入)。

Solution:

解:

Computing a factorial is of course expansive. Though using dynamic programming the computing expanse can be managed, for the large value of n, the factorial value is going exceed normal data size. Needless to say, computing the whole factorial is not the way to find the number of trailing zeros. There must be some advanced algorithm to find the no of trailing zeros.

計(jì)算階乘當(dāng)然是可擴(kuò)展的。 盡管使用動(dòng)態(tài)編程可以管理計(jì)算范圍,但是對(duì)于較大的n值,階乘值將超過正常數(shù)據(jù)大小。 不用說,計(jì)算整個(gè)階乘不是找到尾隨零的數(shù)量的方法 。 必須有一些高級(jí)算法來找到尾隨零 。

Firstly, we need to understand what causes trailing zeroes. A pair of 2 & 5 is the reason behind a trailing zero. Thus a pair of 2 & 5 in the factorial expression leads to a trailing zero. Thus we simply need to check how many pairs (different) of 2 & 5 are there.

首先,我們需要了解導(dǎo)致尾隨零的原因。 一對(duì)2和5是尾隨零后面的原因。 因此,階乘表達(dá)式中的2和5對(duì)導(dǎo)致尾隨零。 因此,我們只需要檢查2和5有多少對(duì)(不同)。

Let's say 5!
5!= 5*4*3*2*1 (thus only one pair)
5!=120 ( only one trailing zero)

假設(shè)5!
5!= 5 * 4 * 3 * 2 * 1 (因此只有一對(duì))
5!= 120 (僅一個(gè)尾隨零)

Intuition says that we don’t even need to find the number of pairs as the occurrence of 2 as a factor is obvious if a 5 is present as a factor. Thus we only need to check how many 5 is there as a factor in the factorial.

直覺說,我們甚至不需要找到對(duì)的數(shù)量,因?yàn)槿绻?作為一個(gè)因素,則2作為一個(gè)因素的出現(xiàn)就很明顯。 因此,我們僅需要檢查階乘中有5個(gè)因素。

Algorithm:

算法:

Set count to 0For(i=5;n/i>0;i=i*5)count=count+ n/i;Return count .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}} .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}

C ++代碼查找數(shù)字階乘中的尾隨零 (C++ code to find trailing zeros in factorial of a number)

#include<bits/stdc++.h> using namespace std;int trailingZeros(int n){int count=0;if(n<0)return -1;for(int i=5;n/i>0;i*=5){count+=n/i;}return count; }int main(){int n;cout<<"enter input,n"<<endl;cin>>n;if(trailingZeros(n))cout<<"no of trailing zero in "<<n<<"! is "<<trailingZeros(n)<<endl;elsecout<<"input is negative, can't proceed"<<endl;return 0; }

Output

輸出量

First run: enter input,n 15 no of trailing zero in 15! is 3Second run: enter input,n 100 no of trailing zero in 100! is 24

翻譯自: https://www.includehelp.com/algorithms/find-trailing-zeros-in-factorial-of-a-number.aspx

求階乘的第一個(gè)非零數(shù)字

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的求阶乘的第一个非零数字_查找数字阶乘中的尾随零的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。