看板 C_and_CPP 關於我們 聯絡資訊
題號:495 遇到的問題: 我用同樣的內容,丟 http://zerojudge.tw/RealtimeStatus 通過, 但是丟 UVa 卻跟我說 Time limit exceeded 我測得時間約 600ms,應該是在時限3秒內才對, 程式碼長度: 1422 Bytes,執行檔大小: 9902 Bytes。 請各位幫我想一下可能是哪出了問題,謝謝。 有問題的code: (請善用置底文的標色功能) /* Fibonacci Freeze */ #include <string.h> #include <iostream> #define LEN 1500 using namespace std; void Print(char n[])//輸出結果 { int i; for( i = LEN - 1 ; i > 0 ; i-- ) if( n[ i ] != 0) break; for( ; i >= 0; i-- ) printf("%d", n[ i ] ); printf("\n"); } void Add(char a[], char b[], char c[])//定義加法 { int i; for( i = 0 ; i < LEN ; i++ ) c[ i ] = a[ i ] + b[ i ] ; for( i = 0 ; i < LEN - 1 ; i++ ) { if( c[ i ] >= 10 ) { c[ i + 1 ] += c[ i ] / 10 ; c[ i ] %= 10; } } } void Fibon( int n, char a[3][LEN] )//定義Fibonacci數的運算 { int j; int i = 1; for( j = 0 ; j < LEN; j++ )//定義0 a[ 0 ][ j ] = 0; for( j = 1 ; j < LEN; j++ )//定義1 a[ 1 ][ j ] = 0; a[ 1 ][ 0 ] = 1; while ( i < n ) { //製作一個 a[3][LEN] 矩陣 Add( a[ 0 ] , a[ 1 ] , a[ 2 ] );//固定用 a[2] 輸出結果 for ( j = 0 ; j < LEN ; j++ ) { a[ 0 ][ j ] = a[ 1 ][ j ]; a[ 1 ][ j ] = a[ 2 ][ j ]; } i++; } } int main() { int n; while( cin >> n ) //連續輸出 { char a[3][LEN]; if ( n == 0 | n == 1) //第0項與第1項分開輸出 { cout << "The Fibonacci number for "<< n << " is " << n << endl; } else { Fibon( n , a ); cout << "The Fibonacci number for "<< n << " is "; Print( a[2] );//固定用 a[2] 輸出結果 } } return 0; } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 219.85.191.230
LawlietDo:答案存起來 不要重算 試試看 08/24 01:00
Yshuan:兩個OJ測資也不同阿= = 08/24 01:04
Cidolfas:如果要用答案存起來的方式,測資大時我程式會爆... 08/24 01:10
Cidolfas:請問2樓是指? 08/24 01:10
bleed1979:動態配置或全域。 08/24 03:16