可用面额-100s,50s和20s
输入应被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

关于java - 没有最低面额值(value)的货币面额组合,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51930702/

10-11 00:28