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 ; }