Java 无最低面额值的货币面额组合

Java 无最低面额值的货币面额组合,java,math,Java,Math,可用面额-100、50和20 输入应能被10整除&大于30 我只是尝试了如下所示,尝试了几乎所有的输入-我是否有办法减少/简化此解决方案 public class Sample { private static void denomCalc(int userVal) { input = userVal; OrgAmt = input; // Validate input for the available denominations

可用面额-100、50和20 输入应能被10整除&大于30

我只是尝试了如下所示,尝试了几乎所有的输入-我是否有办法减少/简化此解决方案

public class Sample {

    private static void denomCalc(int userVal) {

        input = userVal;
        OrgAmt = input;

        // Validate input for the available denominations 100s 50s and 20s
        if (input == 30 || input < 20 || input % 10 != 0) {
            return;
        }

        // check input against %100
        OrgAmt = input % 100;

        // Check if 100s are needed
        if (input > 200) {
            hundereds = (input / 100) - 1;
            OrgAmt = input - hundereds * 100;
        } else if (input > 100 && input < 200) {
            if ((input % 100) % 50 == 0) {
                hundereds = (input / 100);
                OrgAmt = input - hundereds * 100;
                fiftys = (OrgAmt / 50);
                OrgAmt = OrgAmt % 50;
            } else if ((input % 100) % 20 == 0) {
                hundereds = (input / 100);
                OrgAmt = input - hundereds * 100;
                twenties = (OrgAmt / 20);
                OrgAmt = OrgAmt % 20;
            } else {
                OrgAmt = input;
            }
        } else {
            OrgAmt = input;
        }
        // Check if 50s are needed
        if (OrgAmt % 50 < 20) {
            fiftys = (OrgAmt / 50) - 1;
            OrgAmt = OrgAmt - fiftys * 50;
        } else if (OrgAmt % 50 > 20) {
            // Check for 20s
            if ((OrgAmt % 50) % 20 == 0) {
                fiftys = (OrgAmt / 50);
                OrgAmt = OrgAmt - fiftys * 50;
            } else {
                fiftys = (OrgAmt / 50) - 1;
                OrgAmt = OrgAmt - fiftys * 50;
            }
        }
        twenties = (OrgAmt / 20);

        System.out.println(hundereds + " number of 100\'s");
        System.out.println(fiftys + " number of 50\'s");
        System.out.println(twenties + " number of 20\'s");

    }



    public static void main(String[] args) {
        denomCalc(260);
    }

}

这里有一个不太复杂的方法来计算你需要多少笔记。这是伪代码,而不是语法完美的Java。幸运的是,它覆盖了所有有效的输入,这与我之前过于仓促的尝试不同

// before this the input has been checked, as in OP's code

fifties = 0;
if (mod(input,20)==10) {
    fifties = 1;
} 
remainder = input - fifties*50;

// hundreds will be 0 if remainder is less than 100
hundreds = remainder / 100;
remainder = remainder-(hundreds*100);
twenties = remainder / 20;

// now print the results

如果modinput,20==10-条件失败-五十总是忽略为零。