Sunday, 5 August 2012

UVa 166 - Making Change Solution

#include<iostream>
#include<list>
#include<string>
#include<cstring>
#include<sstream>
#include<cctype>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<stack>
#include<fstream>
#include<cstdlib>
#include<vector>
#include<map>
#include<set>
#include<utility>
#include<iomanip>
#include<queue>

using namespace std;

#define INF (1<<29)
#define SET(a) memset(a,-1,sizeof(a))
#define ALL(a) a.begin(),a.end()
#define CLR(a) memset(a,0,sizeof(a))
#define FILL(a,v) memset(a,v,sizeof(a))
#define PB push_back
#define FOR(i,n) for(int i = 0;i<n;i++)
#define PI acos(-1.0)
#define EPS 1e-9
#define MP(a,b) make_pair(a,b)
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define READ freopen("input.txt", "r", stdin)
#define WRITE freopen("output.txt", "w", stdout)
#define LL long long
#define MX 100000
#define MOD 1000000007


LL ways [7500];
int remain[7];
int coin [] = { 5,10,20,50,100,200 };
void count ()
{
    ways [0] = 0;

    for(int i=1;i<7500;i++)     ways[i]=MX;

    for ( int i = 0; i < 6; i++ )
        for ( int j = coin [i]; j < 7500; j++ )
            {
                ways [j] =min(ways[j], 1+ways [j - coin [i]]);
            }
}

int func(int rem, int coin_num)
{
    if(rem<=0 && coin_num<0)
        return ways[-rem];
    else if(coin_num<0)
        return MX;

    else if(!remain[coin_num])
         return func(rem,coin_num-1);
    else
    {
        int y=func(rem, coin_num-1);
        remain[coin_num]--;
        int x=func(rem-coin[coin_num], coin_num);
        remain[coin_num]++;
        return min(1+x,y);
    }
}

main ()
{
    count ();
    int n;
    double exchange;
    //READ;
    //WRITE;
    while(cin>>remain[0]>>remain[1]>>remain[2]>>remain[3]>>remain[4]>>remain[5])
    {
        if(remain[0]==0 && remain[1]==0 && remain[2]==0 && remain[3]==0 && remain[4]==0 && remain[5]==0)
        break;

        cin>>exchange;
        exchange*=100.0;
        //cout<<exchange<<endl;
        n=exchange+.5;
        //cout<<n<<endl;
        cout<<setw(3)<<func(n,5)<<endl;
    }

}

No comments:

Post a Comment