Go 與 D的PK 1

來源:互聯網
上載者:User

標籤:go   d語言   測試   map   

/* 比較一下 

:http://blog.csdn.net/iilovetopview/article/details/43745059

go基本與D語言的運算效率持平。

用“數學公式”法解下題:

 有一個整數n,寫一個函數f(n),返回0到n之間出現的"1"的個數。
  比如f(13)=6,現在f(1)=1,問下一個最大的f(n)=n的n是什嗎?

*/

package mainimport ("fmt""time""math") var aa map[int]intvar Ac []intfunc main() {fmt.Println("(go Language)  Please wait for some minutes,will found Next Fn(n) = n ,the n is:")n := 1count :=0pos := 0t :=time.Now()for n =1;n<200000;n++ {aa = make(map[int]int)    Ac = make([]int,10)pos = Fn(n) if n == pos {count++fmt.Println("n is :",n," Fn(n) is :",pos)if(count >1) {break}}}fmt.Println("time is :",time.Now().Sub(t).String())fmt.Println(n," stop")fmt.Println("test 199981 is :",Fn_test(199981))} func Fn(n int) int {i:=0y:=0for x :=n;x>=1;x/=10 {y=x%10aa[i] =yAc[i] =yi++}Ac = Ac[0:i]  //關鍵m := len(Ac)-1h :=aa[m]if( n%gPow(m) == 0) {return fnA(h,m)}else{ return  fnA(h,m)+fnB(m-1)}}func fnA(h int,m int) int {if(h == 0){return 0}else if h==1 {if(m == 0) {return 1} else{ return  m*gPow(m-1)+1}}else {if(m == 0) {return 1} else{ return  gPow(m)+h*m*gPow(m-1)}}} func fnB(m int) int {   ..... //代碼略return  sum + fnB(m-1)}func gPow(m int) int {return int(math.Pow10(m)) }/*------------測試-------------*/func Fn_test(n int) int { total:= 0for i:=0;i<=n;i++ {total += Count(i)}return total}func Count(n int) int {num:=0for t:= n;t >=1;t=t/10 {if(t%10 == 1) {num++}}return num}


Go 與 D的PK 1

聯繫我們

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