coin-change递归项目说明

tzdcorbm  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(278)

我已经编写了代码,用于计算一个人可以从1到100之间的任何值中使用递归得到的更改的可能性。我不确定项目中的两个方法是什么(代码中的粗体部分),所以有人能给我解释一下吗?我对 java 相当陌生。
我已经包括了上下文的整个代码,但不确定是否有必要。

import java.util.Scanner;
import java.util.ArrayList;

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Scanner keyboard = new Scanner(System.in);
        int[] coins = {1, 5, 10, 25};
        int answer = sc.nextInt();
        if (answer < 1 || answer > 100) {
            throw new IllegalStateException("Invalid value entered: " + answer);
        } else {
            System.out.println(findValue(answer, 0, coins));
        }

    }

    public static int findValue(int n, int current, int[] coins) {
        if (n >= 0 && n < 5) {
            return 1;
        }

        if (n < 0) {
            return 0;
        }

         **int num = 0;
        if (current == 0 && n % 5 != 0 && n > 5) {
            n -= n % 5;
        }

        for (int i = current; i < coins.length; i++) {
            num += findValue(n - coins[i], i, coins);
        }
        return num;**

    }

    public static boolean isNickelPossible(int n) {
        if (n >= 5) {
            return true;
        }
        return false;
    }

    public static int numNickels(int n) {
        int count = 0;
        if (n % 5 == 0) {
            return n / 5;
        }

        while (n - 5 >= 0) {
            n = n - 5;
            count++;
        }
        return count;
    }

    public static boolean isDimePossible(int n) {
        if (n >= 10) {
            return true;
        }
        return false;
    }

    public static int numDimes(int n) {
        int count = 0;
        if (n % 10 == 0) {
            return n / 10;
        }

        while (n - 10 >= 0) {
            n = n - 10;
            count++;
        }
        return count;
    }

    public static boolean isQuarterPossible(int n) {
        if (n >= 10) {
            return true;
        }
        return false;
    }

    public static int numQuarters(int n) {
        int count = 0;
        if (n % 25 == 0) {
            return n / 25;
        }

        while (n - 25 >= 0) {
            n = n - 25;
            count++;
        }
        return count;
    }
}
23c0lvtd

23c0lvtd1#

你必须一行一行地走每一步。顺便说一句,我不认为,目前的代码工作。您可能需要考虑先让代码在没有递归的情况下工作,然后再添加递归。

// seed the current run with the number of coin being removed in this iteration
    int num = 0;
    // current == 0 translates to coins[0] which equals pennies
    // check to see if the number of coins is divisible by 5, because all the coins, other than pennies
    // are divisble by 5
    // the remainder is the number of pennies 
    if (current == 0 && n % 5 != 0 && n > 5) {
        // reduce the current value (n) by the number of pennies
        n -= n % 5;
    }

    // walk the array of available coins, from pennies to quarters and 
    // find the number of coins that can be removed after that value is decremented.
    for (int i = current; i < coins.length; i++) {
        num += findValue(n - coins[i], i, coins);
    }
    return num;

相关问题