Uva 674 ---- coin change
- // coin change
- #include <iostream>
- using namespace std;
- int main()
- {
- int iCoin[7490][5];
- int iMoney[5] = { 1, 5, 10 , 25, 50 };
- int iAmount, i ,j, iMax;
- while ( cin >> iAmount )
- {
- iMax = -1;
- for ( i=0 ; i<=iAmount ; i++ )
- iCoin[i][0] = 1;
- for ( j=0 ; j<5 ; j++ )
- iCoin[0][j] = 1;
- for ( i=1 ; i<=iAmount ; i++ )
- for ( j=1 ; j<5 ; j++ )
- {
- if ( i-iMoney[j] >= 0 )
- iCoin[i][j] = ( iCoin[i][j-1] + iCoin[i-iMoney[j]][j] );
- else
- iCoin[i][j] = iCoin[i][j-1];
- }
- /*
- for ( i=0 ; i<iAmount ; i++ )
- {
- for ( j=0 ; j<5 ; j++ )
- cout << iCoin[i][j] << " ";
- cout <<endl;
- }
- */
- for ( j=0 ; j<5 ; j++)
- if ( iCoin [iAmount][j] > iMax )
- iMax = iCoin[iAmount][j];
- cout << iMax <<endl;
- }
- }
擷取自老師上課投影片
留言
張貼留言