發表文章

目前顯示的是有「Fibonacci」標籤的文章

UVa 900 Brick Wall Patterns

解題: 將直放的箱子視做1,橫放的箱子視作2 多一單位長度的情況為(上一次情況多1(直放))+(上上次情況多2(橫放)) 故為上一次情況和上上次情況的加總 Code: #include<stdio.h> #include<stdlib.h> #include<iostream> #include<algorithm> using namespace std ; long long int f [ 55 ]={ 0 }; long long int fun ( int n ) {     if ( n == 1 ) return f [ 1 ]= 1 ;     else if ( n == 2 ) return f [ 2 ]= 2 ;     if ( f [ n ]> 0 ) return f [ n ];     else return f [ n ]= fun ( n - 1 )+ fun ( n - 2 ); } int main () {     int n ;     fun ( 50 );     while ( cin >> n && n != 0 )     {         cout << f [ n ]<< endl ;     }     return 0 ; }

UVa 12459 Bees` ancestors

解題:費氏數列(DP) Code: #include<stdio.h> #include<stdlib.h> #include<iostream> #include<algorithm> #include<string.h> using namespace std ; long long int ff [ 100 ]; long long int f ( int n ) {     if ( n == 1 ) return ff [ 1 ]= 1 ;     if ( n == 2 ) return ff [ 2 ]= 2 ;     if ( ff [ n ]> 0 ) return ff [ n ];     else return ff [ n ]= f ( n - 1 )+ f ( n - 2 ); } int main () {     int n ;     memset ( ff , 0 , sizeof ( ff ));     while ( cin >> n && n != 0 )     {         long long int ans = f ( n );         cout << ans << endl ;     }     return 0 ; }