Factorial Trailing Zeroes,factorialtrailing

來源:互聯網
上載者:User

Factorial Trailing Zeroes,factorialtrailing

本文是在學習中的總結,歡迎轉載但請註明出處:http://blog.csdn.net/pistolove/article/details/42417535


Given an integer n, return the number of trailing zeroes in n!.

Note: Your solution should be in logarithmic time complexity.

思路:

(1)題意為求解一個整數經過階乘計算得到結果中有多少個0。

(2)我們知道0的個數和2和5有關,而2的個數要遠遠多於5的個數,所以求得5的個數即為0的個數。

(3)但是對於25、125這樣由若干個5相乘組合的,需要計算待求解整數中有多少個這樣的數。

(4)例如對於1000來說,1000/5=20,100/25=40,1000/125=8,1000/625=1,1000/1250=0,即0的個數為20+40+8+1=69個。

(5)希望本文對你有所協助。

演算法代碼實現如下所示:

public static  int trailingZeroes(int n) {int count=0;while(n>0){count = count + n/5;n=n/5;}return count;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.