[Programming question] Piecing together coins (Meituan Dianping 2017 Autumn Recruitment)

Time limit: 1 second
Space limit: 32768K

Give you six banknotes with denominations of 1, 5, 10, 20, 50, and 100 yuan. Assuming that the number of each currency value is sufficient, write a program to find different combinations of N yuan (N is a non-negative integer from 0 to 10000). number of.

Input description:
The input consists of an integer n (1 ≤ n ≤ 10000)

Output description:
output an integer representing the number of different combinations

Input Example 1:
1
Output Example 1:
1

Analysis:
The first thing that comes to mind is the violent recursive method:
1. Use 0 pieces of 1 yuan currency, let [5, 10, 20, 50, 100] form the remaining N, and the final method number is recorded as res1.
2. Use 1 piece of 1 yuan currency, let [5, 10, 20, 50, 100] form the remaining N - 1, and the final method number is recorded as res2.
3. Use 2 pieces of 1 yuan currency, let [5, 10, 20, 50, 100] form the remaining N - 2, and the final method number is recorded as res3.

N. Use N pieces of 1 yuan currency, let [5, 10, 20, 50, 100] form the remaining 0, and the final method number is recorded as resN.

import java.util.*;
public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int aim = sc.nextInt();
            int index = 0;
            long sum = count(aim,index);
            System.out.println(sum);
        }
    }
    public static long count(int aim, int index){
        int[] arr = {1,5,10,20,50,100};
        long res = 0;
        if(index == arr.length){
            res = aim == 0 ? 1 : 0;
        } else {
            for(int i = 0; arr[index] * i <= aim; i++ ) {
                res += count(aim - arr[index] * i, index + 1);
            }
        }
        return res;
    }
}

But this method takes too long, so it is necessary to improve the
dynamic programming method:

import java.util.*;
public class Main{
    public static void main(String[]args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()) {
            int aim = sc.nextInt();
            int[] arr = {1, 5, 10, 20, 50, 100};
            System.out.println(count(arr,aim));
        }
    }
    public static long count(int[]arr, int aim) {
        long[][] dp = new long[arr.length][aim + 1];
        for(int i = 0; i < arr.length; i++) {
            dp[i][0] = 1;
        }
        for(int j = 1; arr[0] * j <= aim; j++) {
            dp[0][arr[0] * j] = 1;
        }
        long num = 0;
        for(int i = 1; i < arr.length; i++) {
            for(int j = 1; j <= aim; j++) {
                num = 0;
                for(int k = 0; j - arr[i] * k >= 0; k++) {
                    num += dp[i - 1][j - arr[i] * k];
                }
                dp[i][j] = num;
            }
        }
        return dp[arr.length - 1][aim];
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325817870&siteId=291194637
Recommended