[ACM ] 107 cat in the hat

看板C_and_CPP (C/C++)作者 (沒有早知道這回事)時間16年前 (2009/07/07 01:12), 編輯推噓1(102)
留言3則, 2人參與, 最新討論串1/1
請大家幫我review一下 看是哪一部份會超出時間 一直TLE 很沮喪 謝謝 //----------------------------- #include<stdio.h> #include<math.h> int main(){ long initial_cat_height, total_cat_height; long total_worker_cat, total_not_worker_cat; long i,j,count; printf( "%ld %ld\n" , (long)pow(9999,2),(long)pow(10000,2) ) ; while(scanf("%ld %ld",&initial_cat_height,&total_worker_cat) == 2){ count = 0; i = 2; total_cat_height = 0; if(initial_cat_height == 0 && total_worker_cat == 0) return 0; if(total_worker_cat == 1) i = 1; if( initial_cat_height == 1 ) { printf( "0 %ld\n" , total_worker_cat ) ; } else { for(j = 0;;j++){ if((long)pow(i,j) == total_worker_cat && (long)pow(i+1,j) == initial_cat_height){ break; } if(pow(i,j) > total_worker_cat){ i++; j = 0; } }/* i for N and j for counts (numbers of iterations)*/ if(i == 1) total_not_worker_cat = j; else total_not_worker_cat = (long)((1 - pow(i,j)) / (1 - i)); while(j >= 0){ // total_not_worker_cat += (long)( pow(i,j)); total_cat_height += (long)(pow(i,j)/pow(i+1,j)*initial_cat_height); j--; } printf("%ld %ld\n",total_not_worker_cat,total_cat_height); } } return 0; } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 124.8.78.129

07/07 01:12, , 1F
PS 跑了一些很大的資料也是都OK的說
07/07 01:12, 1F

07/07 01:12, , 2F
還是有什麼會陷入迴圈的CASE我沒注意到呢@@
07/07 01:12, 2F

07/07 23:38, , 3F
測測看 => 2147483647 2147483646
07/07 23:38, 3F
文章代碼(AID): #1AKZ1aAB (C_and_CPP)
文章代碼(AID): #1AKZ1aAB (C_and_CPP)