达到不同整数的数字(Java) 这是一个问题:考虑一个有七个面额的钞票系统,即RS 1、RS 2、RS 5、Rs. 10、Rs. 50、Rs 100。如果通过键盘输入了一个Rs.N的总和,那么编写一个程序来计算最小数量的音符,这些音符组合起来可以得到Rs.N

达到不同整数的数字(Java) 这是一个问题:考虑一个有七个面额的钞票系统,即RS 1、RS 2、RS 5、Rs. 10、Rs. 50、Rs 100。如果通过键盘输入了一个Rs.N的总和,那么编写一个程序来计算最小数量的音符,这些音符组合起来可以得到Rs.N,java,algorithm,Java,Algorithm,这是我的密码。程序似乎陷入了无限循环 public class ReachNwithCurrencyNotes { public static void main(String[] args) { int n,temp = 0, a = 1, b = 2, c = 5, d = 10, e = 50, f = 100, sum = 0, countA = 0, countB = 0, countC = 0, countD = 0, countE = 0, countF

这是我的密码。程序似乎陷入了无限循环

public class ReachNwithCurrencyNotes {

    public static void main(String[] args) {

        int n,temp = 0, a = 1, b = 2, c = 5, d = 10, e = 50, f = 100, sum = 0, countA = 0, countB = 0, countC = 0, countD = 0, countE = 0, countF = 0;

        Scanner input = new Scanner (System.in);

        System.out.println("Please enter N");

        n = input.nextInt();

        temp = n;

        do {

            if (temp-f>=100) {
                sum = sum + f;
                countF++;
                temp = temp - f;
            }
            else if (temp-e>=50) {
                sum = sum + e;
                countE++;
                temp = temp - e;
            }
            else if (temp-d>=10) {
                sum = sum + d;
                countD++;
                temp = temp - d;
            }
            else if (temp-c>=5){
                sum = sum + c;
                countC++;
                temp = temp - c;
            }
            else if (temp-b>=2){
                sum = sum + b;
                countB++;
                temp = temp - b;
            }
            else if (temp-a>=1){
                sum = sum + a;
                countA++;
                temp = temp - a;
            }
        } while (sum<n);
        System.out.println("N completed with\n" + countA + "\n" + countB + "\n" + countC + "\n" + countD + "\n" + countE + "\n" + countF);
    }
}
公共类ReachNwithCurrencyNotes{
公共静态void main(字符串[]args){
int n,temp=0,a=1,b=2,c=5,d=10,e=50,f=100,和=0,countA=0,countB=0,countC=0,countD=0,countE=0,countF=0;
扫描仪输入=新扫描仪(System.in);
System.out.println(“请输入N”);
n=input.nextInt();
温度=n;
做{
如果(温度f>=100){
sum=sum+f;
countF++;
温度=温度-f;
}
否则,如果(温度e>=50){
sum=sum+e;
countE++;
温度=温度-e;
}
否则,如果(温度d>=10){
sum=sum+d;
countD++;
温度=温度-d;
}
否则,如果(温度c>=5){
sum=sum+c;
c++;
温度=温度-c;
}
否则如果(温度b>=2){
sum=sum+b;
countB++;
温度=温度-b;
}
否则如果(温度a>=1){
sum=sum+a;
countA++;
温度=温度-a;
}

}而(sum您所有的条件检查是否支付
a,b,…
将留下一个严格的正(
=1
)数量。因此,您将永远不会支付最后需要的账单,并且您的总和永远不会达到
n
,从而形成无限循环


例如,如果您可以通过支付
5
达到
0
,您应该这样做。

假设数字为1,则temp-a等于0,因此不会发生任何事情,程序将永远循环

所有条件(如(temp-b>=2)看起来都不正确,请尝试用以下内容替换它们:

(temp-b>=0)
或者干脆

(temp>=b)

为了扩展我的评论,这里有一个例子:

你从你拥有的最高值开始,然后下降到最低值

 //input: integer value 'in'
 int rest = in % 100;
 int n100 = in / 100;
 int n50 = rest / 50;
 rest %= 50;
 int n10 = rest / 10;
 rest %= 10;
 int n2 = rest / 2;
 rest %= 2;
 int n1 = rest % 2;
如果输入273:

   1: rest = 273 % 100 = 73
   2: n100 = 273 / 100 = 2
   3: n50 = 73 / 50 = 1
   4: rest = 73 % 50 = 23
   5: n10 = 23 / 10 = 2
   6: rest = 23 % 10 = 3
   7: n2 = 3 / 2 = 1
   8: rest = 3 % 2 = 1
   9: n1 = 1 

如果
%
是一个选项,那么我将使用它。