在java中更改贪婪算法 [英] min change greedy algorithm in java

查看:131
本文介绍了在java中更改贪婪算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,所以我需要一个程序,要求我一笔钱,然后我需要它告诉我最少的硬币。我可以使用的硬币是:美元,季度,米,镍和便士。例如,当我运行程序时,应该是这样的:

 >运行硬币
输入给定的金额:
[1.73]
给卖家8个硬币:
1美元,
2季度,
2角钱,
0 nickels,
3便士。

这是我迄今为止所做的:

  import java.util.Scanner; 

class硬币{
public static void main(String args []){
扫描仪输入=新的扫描仪(System.in);
双重钱;
System.out.println(输入金额);
money = input.nextDouble();




while(money> 0.0){
if(money> = 1){
System.out.println (钱/ 1 + 元);
钱 - = 1;

}
else if(money> =。25){
System.out.println(money / .25 +quarters);
money - =。25;

}
else if(money> = =。10){
System.out.println(money / .10 +Dimes);
money - =。10;
}
else if(money> = =。05){
System.out.println(money / .05 +Nickels);
money - =。05;
}
else if(money> =。01){
System.out.println(money / .01 +Penny);
money - =。01;
}
}
}
}

我需要帮助的部分是这样的:如果我运行程序并输入数量1.73,我编写代码的方式,它需要数字1.73,将其除以1,并打印1.73美元。我需要一种摆脱小数部分的方式,而不是打印1.73美元,它打印1美元。但我不知道该怎么做我尝试将其转换为int,但会混淆其他语句的流。请帮助我。

解决方案

你需要摆脱分裂后的剩余部分。您可以使用 Math.floor() 为此:

 类硬币{
public static void main(String args []){

double money = 1.73;

int dollars =(int)Math.floor(money / 1);
钱 - =美元* 1;

int quarters =(int)Math.floor(money / 0.25);
钱 - =宿舍* 0.25;

int dimes =(int)Math.floor(money / 0.10);
money - = dimes * 0.10;

int nickels =(int)Math.floor(money / 0.05);
钱 - =镍* 0.05;

int pennies =(int)Math.round(money * 100);

System.out.println(美元:+美元);
System.out.println(Quarters:+ quarters);
System.out.println(Dimes:+ dimes);
System.out.println(Nickels:+ nickels);
System.out.println(Pennies:+便士);
}
}

导致:

 美元:1 
季度:2
Dimes:2
镍:0
Pennies:3


Ok so i need to make a program to ask me for an amount of money, then I need it to tell me the least amount of coins to make it. The coins I can use are: dollars, quarters, dimes, nickels, and pennies. For example, When I run the program it's supposed to look like this:

> run Coins
Enter the amount of given money:
 [1.73]
Give the seller 8 coins:
1 dollars,
2 quarters,
2 dime,
0 nickels,
3 pennies.

This is What I have so far:

import java.util.Scanner;

class Coins {
  public static void main (String args[]) {
     Scanner input = new Scanner(System.in);
     double  money;
     System.out.println("Enter the amount of money ");
     money = input.nextDouble();




     while (money > 0.0 ){
       if (money >= 1) {
          System.out.println(money/1+"dollars");
          money -= 1;

     }
       else if (money>=.25) {
         System.out.println(money/.25+"quarters");
         money-=.25;

       }
       else if (money>=.10) {
         System.out.println(money/.10+"Dimes");
         money-=.10;
       }
       else if (money>=.05) {
         System.out.println(money/.05+"Nickels");
         money-=.05;
       }
       else if (money>=.01) {
         System.out.println(money/.01+"Penny");
         money-=.01;
       }
     }        
  }
}  

The part I need help with is this: If I run the program and enter the amount 1.73, the way I have the code written, it takes the number 1.73, divides it by 1, and prints "1.73 dollars". I need a way to get rid of the decimal part so instead of printing "1.73 dollars", it prints "1 dollar". But I'm not sure how to do this. I tried converting it to an int but it messes up the flow of the other statements. Please help me.

解决方案

You need get rid of the remainder after the divisions. You can use Math.floor() for this:

class Coins {
  public static void main (String args[]) {

     double  money = 1.73;

    int dollars = (int) Math.floor(money/1);
    money -= dollars * 1;

    int quarters = (int) Math.floor(money/0.25);
    money -= quarters * 0.25;

    int dimes = (int) Math.floor(money/0.10);
    money -= dimes * 0.10;

    int nickels = (int) Math.floor(money/0.05);
    money -= nickels * 0.05;

    int pennies = (int) Math.round(money * 100);

    System.out.println("Dollars: " + dollars);
    System.out.println("Quarters: " + quarters);
    System.out.println("Dimes: " + dimes);
    System.out.println("Nickels: " + nickels);
    System.out.println("Pennies: " + pennies);
  }
}

Resulting in:

Dollars: 1
Quarters: 2
Dimes: 2
Nickels: 0
Pennies: 3

这篇关于在java中更改贪婪算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆