n! 的結果中包含多少個0(Go語言實現)

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

n! 的結果中包含多少個0(Go語言實現)

詳細解釋見代碼注釋

package mainimport ("fmt")type myInt int32/**統計階乘n!結果中0的個數*方法1:統計5出現的次數*如25出現了兩個5*因為任意一個偶數都可以和5組成10*而且偶數的量是足夠的 */func fZereCount1(n myInt) myInt {var count, i myInt = 0, 0//完全枚舉for ; n >= 5; n-- {i = n//i值是不會等於0的,所以無需加上&& i > 0for i%5 == 0 {count++i /= 5}}return count}/**方法1的第二種寫法 */func fZereCount1x(n myInt) myInt {var i, count, tmp myInt = 1, 0, 0//部分枚舉for {if 5*i <= n {count++tmp = i//有可能i值也滿足條件for tmp%5 == 0 {count++tmp /= 5}i++} else {return count}}return count}/**方法2:快速統計/*例如:199*count=[199/5]+[199/5/5]+[199/5/5/5]*解釋:[199/5]即求得199內有多少個可以被5整除(取整)*  [199/5/5]即求得199內有多少個可以被25整除(取整)*  [199/5/5]即求得199內有多少個可以被125整除(取整)*特殊數字:如125,既可以被5、25還可以75整除,所以需要被統計3次*綜上所述:此法本質上還是在統計5的個數*/func fZereCount2(n myInt) myInt {var count, tmp myInt = 0, 0for {tmp = n / 5if tmp > 0 {n = tmpcount += tmp} else {return count}}return count}/**代碼作者:天之  部落格:http://blog.csdn.net/WAPWO?viewmode=contents */func main() {fmt.Println(fZereCount1(10000))fmt.Println(fZereCount1x(10000))fmt.Println(fZereCount2(10000))}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.