求階乘的第一個非零數字_查找數字階乘中的尾隨零

求階乘的第一個非零數字

Problem statement:

問題陳述:

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

n中找到尾隨零的數目 (其中, 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.

計算階乘當然是可擴展的。 盡管使用動態編程可以管理計算范圍,但是對于較大的n值,階乘值將超過正常數據大小。 不用說,計算整個階乘不是找到尾隨零的數量的方法 。 必須有一些高級算法來找到尾隨零

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.

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

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

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

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.

直覺說,我們甚至不需要找到對的數量,因為如果5作為一個因素,則2作為一個因素的出現就很明顯。 因此,我們僅需要檢查階乘中有5個因素。

Algorithm:

算法:

    Set count to 0
For(i=5;n/i>0;i=i*5)
count=count+ n/i;
Return count

C ++代碼查找數字階乘中的尾隨零 (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;
else
cout<<"input is negative, can't proceed"<<endl;
return 0;
}	

Output

輸出量

First run:
enter input,n
15 
no of trailing zero in 15! is 3
Second 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

求階乘的第一個非零數字

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/542151.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/542151.shtml
英文地址,請注明出處:http://en.pswp.cn/news/542151.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

高速緩存dns

1. DNS&#xff1a; Domain Name System&#xff0c;域名系統。 萬維網上作為域名和IP地址相互映射的一個分布式數據庫&#xff0c;能夠使用戶更方便的訪問互聯網。他主要負責把域名和IP的相互轉換&#xff0c;DNS運行與TCP|UDP的53端口上。 2. 高速緩存DNS&#xff1a;DNS服務…

python log日志級別_python – 日志記錄:如何為處理程序設置最大日志級別

您可以向文件處理程序添加過濾器.這樣,您可以將特定級別重定向到不同的文件.import loggingclass LevelFilter(logging.Filter):def __init__(self, low, high):self._low lowself._high highlogging.Filter.__init__(self)def filter(self, record):if self._low < recor…

Python Pandas –合并,聯接和串聯

There are three main ways to combine dataFrames i.e., merging, joining and concatenating. The following examples will illustrate merging, joining and concatenation. 組合dataFrames的主要方法有三種&#xff0c;即合并&#xff0c;聯接和串聯 。 以下示例將說明合并…

Apache服務配置

1. apache 企業中常用的web服務。用來提供http&#xff1a;//&#xff08;超文本傳輸協議&#xff09; 基礎信息&#xff1a; 主配置目錄&#xff1a; /etc/httpd/conf 主配置文件&#xff1a; /etc/httpd/conf/httpd.conf 子配置目錄&#xff1a; /etc/httpd/conf.d/ 子配置文…

git 怎么查看合并過來哪些代碼_git整理紛亂的歷史合并記錄

https://github.com/Epix37/Hearthstone-Deck-Tracker以上面版本庫的master分支為例父節點1SHA-1: a21142968282ae49720cf30a0f18290b2ce74b3a* remove hotkey from config if action could not be found, fix hotkey menu item name父節點2SHA-1: 86a824e8f46005db91f334dfc57…

如何安裝Genymotion虛擬機以及Genmotion的eclipse插件

---內容開始--- - 首先去genymotion的官網去下載其安裝文件 資源下載 Genymotion官網必須注冊一個賬號這個賬號安裝之后還有用的&#xff0c;用戶名最好用網易126郵箱注冊----我下載的是2.8.0的版本(注&#xff1a;注冊前先開個代理服務器不然頁面打不開下載時最好用迅雷下載這…

java system類_Java System類mapLibraryName()方法及示例

java system類系統類mapLibraryName()方法 (System class mapLibraryName() method) mapLibraryName() method is available in java.lang package. mapLibraryName()方法在java.lang包中可用。 mapLibraryName() method is used to map a given library name into a platform-…

squid服務配置(正向、反向代理)

代理&#xff1a; 就是代理網絡用戶去取得網絡信息。 Squid是一種用來緩沖Internet數據的軟件。安裝Squid服務實現代理緩存服務器功能。 正向代理&#xff1a;意思是一個位于客戶端和原始服務器之間的服務器&#xff0c;為了從原始服務器取得內容&#xff0c;客戶端向代理發送一…

家譜整站源碼php_mysql家譜表查詢某人所有后代

CREATE TABLE people (id INT(11) NOT NULL,name VARCHAR(50) NULL DEFAULT NULL,pid INT(11) NOT NULL DEFAULT 0,PRIMARY KEY (id));CREATE DEFINERroot% PROCEDURE getChildren(IN parentId INT)LANGUAGE SQLNOT DETERMINISTICCONTAINS SQLSQL SECURITY DEFINERCOMMENT 獲取…

React 入門學習筆記2

摘自阮一峰&#xff1a;React入門實例教程&#xff0c;轉載請注明出處。 一、獲取真實的DOM節點 組件并不是真實的 DOM 節點&#xff0c;而是存在于內存之中的一種數據結構&#xff0c;叫做虛擬 DOM &#xff08;virtual DOM&#xff09;。只有當它插入文檔以后&#xff0c;才會…

c語言getchar函數_C語言中帶有示例的getchar()函數

c語言getchar函數C語言中的getchar()函數 (getchar() function in C) The getchar() function is defined in the <stdio.h> header file. getchar()函數在<stdio.h>頭文件中定義。 Prototype: 原型&#xff1a; int getchar(void);Parameters: FILE *filename(f…

python及pycharm

1.python簡介&#xff1a; Python是一種計算機程序設計語言。是一種動態的、面向對象的腳本語言&#xff0c;最初被設計用于編寫自動化腳本(shell)&#xff0c;隨著版本的不斷更新和語言新功能的添加&#xff0c;越來越多被用于獨立的、大型項目的開發。 python最重要的功能&am…

anaconda如何更改環境配置_手把手教新手安裝Anaconda配置開發環境

Anaconda是針對Python的集成環境&#xff0c;它已經成為全球數千萬數據科學從業人員必備的開發工具&#xff0c;幫助人們有效地解決數據科學和機器學習相關地問題。如果你想從事數據科學和機器學習的工作&#xff0c;可以從本文開始&#xff0c;了解一下如何安裝Anaconda。1. 初…

詳解摘要認證

1. 什么是摘要認證摘要認證與基礎認證的工作原理很相似&#xff0c;用戶先發出一個沒有認證證書的請求&#xff0c;Web服務器回復一個帶有WWW-Authenticate頭的響應&#xff0c;指明訪問所請求的資源需要證書。但是和基礎認證發送以Base 64編碼的用戶名和密碼不同&#xff0c;在…

Python的基礎知識

1.注釋&#xff1a; #單行注釋ctrl / 批量注釋&#xff0c;選中需要注釋的所有行ctrl / 批量取消注釋&#xff0c;選中已經被注釋的所有行 塊注釋&#xff1a;上下各三個雙引號的部分全部被注釋 “”“ hello haha ”“”2.變量&#xff1a; 變量命名的規則&#xff1a; …

樹莓派該文件名_樹莓派:文本編輯器與文件

GNU nano是Unix系統下一款常用的文本編輯器&#xff0c;以簡單易用著稱。與之相比&#xff0c;功能更強大的Vi和Emacs編輯器&#xff0c;學習曲線比nano陡峭很多。由于nano對于一般的文本編輯來說已經足夠&#xff0c;所以我想簡單介紹一下&#xff0c;以便于更好入門。基本使用…

Java SimpleTimeZone setStartYear()方法與示例

SimpleTimeZone類setStartYear()方法 (SimpleTimeZone Class setStartYear() method) setStartYear() method is available in java.util package. setStartYear()方法在java.util包中可用。 setStartYear() method is used to set the DST (Daylight Savings Time) starting y…

報表在IBM AIX系統下resin部署

&#xfeff;&#xfeff;報表是用java開發的&#xff0c;具有良好的跨平臺性。不僅可以應用在windows、linux、操作系統&#xff0c;還可以應用在AIX等等的unix操作系統。在各種操作系統上部署過程有一些差別。下面說一下在AIX操作系統的部署的步驟。 1. 首先&#xff0c;下載…

python——if語句、邏輯運算符號

1.if條件判斷語句&#xff1a; if 要判斷的條件(True):條件成立的時候&#xff0c;要做的事情 elif 要判斷的條件(True):.... elif 要判斷的條件(True):.... else:條件不成立的時候要做的事情示例&#xff1a; 判斷學生分數等級&#xff1a; 100——90&#xff08;包括90&…

sox處理mp3_使用SoX將mp3文件拆分為TIME秒

I need to split mp3 file into slices TIME sec each. Ive tried mp3splt, but it doesnt work for me if output is less than 1 minute.Is it possible do do with:sox file_in.mp3 file_out.mp3 trim START LENGTHWhen I dont know mp3 file LENGTH解決方案You can run SoX…