[ACM ] 100 3n+1 problem 如何再改善?

看板C_and_CPP作者 (丁丁)時間15年前 (2009/03/20 01:28), 編輯推噓2(202)
留言4則, 4人參與, 最新討論串1/2 (看更多)
小弟練了一年 終於把ACM 最簡單的3n+1 problem寫出來 顯示AC了 不過爬了一下文 這題好像有好多種解 想請問我這樣寫 有沒有什麼不妥的地方呢? 而且花了1.26秒....好久@@ ======================================================= #include<stdio.h> #include<stdlib.h> int length(int n); int main() { int a=0,b=0,temp=0,max,i,index; while( (scanf("%d %d",&a,&b))!=EOF ) { printf("%d %d ",a,b); if(a>b) { temp=a; a=b; b=temp; } max=length(a); for(i=a;i<=b;i++) { if(length(i)>max) { max=length(i); index=i; } } //for printf("%d\n",max); } //while system("pause"); return 0; } int length(int n) //傳回整數n的cycle-length值 { int count=1; while(1) { if(n==1) { return count; } else if(n%2==1) { count++; n=3*n+1; continue; } else { count++; n=n/2; continue; } } } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.113.91.19

03/20 01:34, , 1F
把算過的答案記起來
03/20 01:34, 1F

03/20 01:39, , 2F
請問是什麼意思呢? 每個cycle-length都會算到啊
03/20 01:39, 2F

03/20 02:17, , 3F
keyword: 建表
03/20 02:17, 3F

03/20 06:26, , 4F
請參考我的網站解法 World of bleed1979
03/20 06:26, 4F
文章代碼(AID): #19me2ZNS (C_and_CPP)
文章代碼(AID): #19me2ZNS (C_and_CPP)