Pages

Thursday 22 November 2012

UVA - 674 - Coin Change

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuffer sb = new StringBuffer("");
        String m = "";
        int coins[] = {50,25,10,5,1};
        long arr[] = count(coins, coins.length, 7489);
        while ((m = br.readLine()) != null) {
            int x=Integer.parseInt(m);
            sb.append(arr[x]).append("\n");
        }
        System.out.print(sb);
    }

    static long[] count(int coins[], int m, int n) {
        long[] temp = new long[n + 1];
        temp[0] = 1;
        for (int i = 0; i < m; i++) {
            for (int j = coins[i]; j < n+1; j++) {
                temp[j] += temp[j - coins[i]];
            }
        }
        return temp;
    }
}

No comments:

Post a Comment