有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java硬币变化递归项目澄清

我编写了一些代码,用于计算使用递归从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;
    }
}

共 (1) 个答案

  1. # 1 楼答案

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

        // 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;