「java找钱」java找钱问题递归

博主:adminadmin 2022-12-11 11:15:08 74

本篇文章给大家谈谈java找钱,以及java找钱问题递归对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

用JAVA找钱

大哥我是问你不论25分,10分,5分 和1分出现次数只要 =钱数就行?

import java.util.Vector;

public class Test {

Vector sub = new Vector();

Vector count = new Vector();

public Test(){

sub.add(25);

sub.add(10);

sub.add(5);

sub.add(1);

count = parse(100);

result();

}

public Vector parse(int num){

for(int i=0; isub.size(); i++){

int tempSub = Integer.parseInt(sub.elementAt(i).toString());

int c = num/tempSub;

num = num - c*tempSub;

count.add(c);

}

return count;

}

public void result(){

for(int i=0; isub.size();i++){

System.out.println(sub.elementAt(i).toString()+":"+count.elementAt(i).toString()+"个");

}

}

public static void main(String args[]){

new Test();

}

}

有啥不合理的自己改下

应该怎么用java解决,大概的流程,谢谢!

学生操作2:

public static void main(String[] args) {

int shirtPrice = 245; //T恤价格

int shoePrice = 570; //网球鞋价格

int padPrice = 320; //网球拍价格

int shirtNo=2; //T恤件数

int shoeNo =1; //运动鞋数目

int padNo = 1; //网球拍数目

double discount = 0.8;

/*计算消费总金额*/

double finalPay = (shirtPrice * shirtNo + shoePrice * shoeNo + padPrice * padNo) * discount;

//System.out.println("消费总金额:" + finalPay);

/*计算找钱*/

double returnMoney = 1500-finalPay;

/*打印购物小票*/

System.out.println("********消费单********");

System.out.println("购买物品\t" + "单价\t" + "个数\t"+ "金额\t");

System.out.println("T恤\t" + "¥"+shirtPrice+ "\t" + shirtNo+ "\t" + "¥"+(shirtPrice * shirtNo)+"\t");

System.out.println("网球鞋\t" + "¥"+shoePrice + "\t"+ shoeNo+ "\t" + "¥"+(shoePrice * shoeNo)+ "\t");

System.out.println("网球拍\t" + "¥"+padPrice + "\t"+ padNo+ "\t" + "¥"+(padPrice * padNo)+ "\t\n");

System.out.println("折扣:\t8折");

System.out.println("消费总金额\t" + "¥" + finalPay);

System.out.println("实际交费\t¥1500");

System.out.println("找钱\t" + "¥" + returnMoney);

/*计算本次购物所获积分*/

int score = (int)finalPay / 100 * 3;

System.out.println("本次购物所获的积分是: " + score);

}

学生操作3:

mport java.util.Scanner;

public class GookLuck {

/*

* 幸运抽奖

*/

public static void main(String[] args) {

int custNo; // 客户会员号(说明:customer---客户)

// 输入会员卡号

System.out.println("请输入4为会员卡号:");

Scanner input = new Scanner(System.in);

custNo = input.nextInt();

// 获得每位数字

int gewei = custNo % 10; // 分解获得个位数

int shiwei = custNo / 10 % 10; // 分解获得十位数

int baiwei = custNo / 100 % 10; // 分解获得百位数

int qianwei = custNo / 1000; // 分解获得千位数

// 计算数字之和

int sum = gewei + shiwei + baiwei + qianwei;

System.out.println("会员卡号" + custNo + "各位之和: " + sum);

}

找零钱问题 [贪心算法](java实现)

public getMin{

public int MinNumber=0;

public int findMax(int[] a){

for(int i=0;ia.length;i++){

if(a[i]==0) return a[--i];

}

return a[a.length-1];

}

public boolean Compare(int a,int b){

public boolean flag=true;

if(ab) flag=flase;

return flag;

}

public int getMinNumber(int[] M,int Money){

int[] findM=new int[M.length];

int index=0;

for(int i=0;iM.length;i++){

boolean f = this.Compare(M[i],money)

if(f) findM[index++]=M[i];

}

int max = this.findMax(findM);

MinNumber++;

if((Money-max)!=0) {

getMinNumber(M,Money-max)

}

return MinNumber;

}

public int[] Start(){

System.out.println("请输入查询组数");

int group=System.in.read();

int[] M={1,2,5,10,20,50,100};

int[] Result = new Int[group];

int index=0;

while (group-- 0){

System.out.println("请输入金额");

int money=System.in.read();

Result[index++] = getMinNumber(M,money);

MinNumber=0;

}

}

public void print(int[] MinNumber){

for(int i=0;iMinNumber.length.i++){

System.out.println(MinNumber[i]+" ");

}

}

}

public static void main(String[] args){

new getMin().print(new getMin().Start());

}

没测试啊,有问题请勿喷,呵呵

怎么用JAVA编译升级购物结算? 有商品名称、商品金额、继续购买的循环、折扣、总计金额、交费金额、找钱

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.text.DecimalFormat;

import org.apache.commons.lang.StringUtils;

public class Test

{

public static void main(String []args){

System.out.println("******************************************************************************************************");

System.out.println("请选择要购买的商品编号:");

System.out.println("1.T恤(245) 2.网球鞋(570) 3.网球拍(320)");

System.out.println("******************************************************************************************************");

Long total = 0L;

//应付费用和找零保留两位小数点

DecimalFormat df = new DecimalFormat("#.00");

BufferedReader strin=new BufferedReader(new InputStreamReader(System.in));

//接受用户输入的变量,商品编码、商品数量、是否继续和实际交费

String code = "";String num = "";String isBuy = "";String pay = "";

Double discount = 0.8;

int number = 0;Long payMoney = 0L;Boolean flag = false;

try

{

//do...while循环,先执行do,然后在用户输入y的情况下循环执行do中的语句

do{

code ="";num="";isBuy="";

//如果输入编号有误则循环重新输入

do{

flag=false;

System.out.print("请输入商品编号:"+code);

//获取用户输入的编码

code = strin.readLine();

if(code.equals("1")||code.equals("2")||code.equals("3")){

flag = true;

}else{

flag=false;

System.out.print("商品编号不存在!");

}

}while(flag == false);

//输入不符合要求时循环重新输入

do{

flag = false;

System.out.print("请输入商品数量:"+num);

//获取用户输入的数量

num = strin.readLine();

//在输入数量不为空的情况下,将数量转换成整型,若转换失败说明输入的不是整型数据,提示用户重新输入

if(StringUtils.isNotEmpty(num)){

try

{

number = Integer.parseInt(num);

flag = true;

}

catch (Exception e)

{

System.out.println("商品数量请输入正整数!");

flag = false;

num = "";

}

}

}while(flag == false);

//根据用户输入情况计算当前费用

if(code.equals("1")){

total += 245*number;

System.out.println("T恤 ¥"+245.0 +" ¥"+total);

System.out.print("是否继续(y/n)"+isBuy);

isBuy = strin.readLine();

}else if(code.equals("2")){

total+=570*number;

System.out.println("网球鞋 ¥"+570.0 +" ¥"+total);

System.out.print("是否继续(y/n)"+isBuy);

isBuy = strin.readLine();

}else if(code.equals("3")){

total+=361*number;

System.out.println("网球拍 ¥"+320.0 +" ¥"+total);

System.out.print("是否继续(y/n)"+isBuy);

isBuy = strin.readLine();

}

}while(isBuy.equals("y"));

System.out.println("折扣:"+discount);

//根据折扣计算顾客需要交纳的费用

String needPay = df.format(total*discount);

System.out.println("金额总计: ¥"+needPay);

//输入不符合要求时循环重新输入

do{

flag =false;

System.out.print("实际交费: ¥"+pay);

//获取用户输入的实际交费

pay = strin.readLine();

//在输入交费不为空的情况下,将交费转换成长整型,若转换失败说明输入的不是长整型数据,提示用户重新输入

if(StringUtils.isNotEmpty(pay)){

try{

payMoney = Long.parseLong(pay);

flag=true;

}catch(Exception ex){

System.out.println("实际交费请输入数字!");

flag=false;

pay="";

}

}

}while(flag == false);

System.out.print("找钱: ¥"+(payMoney-Double.parseDouble(needPay)));

}catch (IOException e1){

e1.printStackTrace();

}

}

}

以上是能达到输出要求的代码,有通过测试,比较初级的,希望能帮到楼主。

JAVA 递归找零钱

看了楼上的写法后,不甚满意,于是自己写了一种。

这个题目很有意思,相当有意思。

加上了比较详细的注释。

你试一试。

这种方法你能很明显的看出递归的特征。

可以随意传要找钱的总数,以及硬币面额可以增加或者减少。但是

硬币的面额要由小到大排列,就ok

注释加的有点多。。看起来有点累赘。不过能容易看。

package file;

import java.util.ArrayList;

import java.util.List;

/**

* 递归得到找钱方案个数

* 思路为:

* 定义待找钱币类型。默认从小到大排列。

* 传入参数后,先获得最大的硬币面值。通过整除,得到可以有几种找钱情况

* 递归调用找钱,因为排除了最大面额硬币后的可找硬币以余额都发生了变化。可以看作一次新的找钱

* 最终得到所有的找钱方案。封装到List中返回。

* @version TestRePay.java v. 1.0.0 2010-10-26

* @author Andy

*/

public class TestRePay {

public static void main(String[]args){

int i = 150;

int[] coinType = new int[]{1,5,10,25,50};

//调用递归方法

ListString allResult = reCoins(coinType, i);

System.out.println("找钱方案的个数为:"+allResult.size());

for(int j = 1 ,k = allResult.size(); j=k;j++ ){

System.out.println(" 第"+j+"种找钱方案为:"+allResult.get(j-1));

}

}

/**

* 通过递归,得到所有的方案数量

* author Andy

* date 2010-10-26 下午01:30:06

* @param coinType 可以找给客户的钱币的类型 为int数组

* @param totalMoney 待找钱的数量

* @return

*/

public static ListString reCoins(int[]coinType , int totalMoney){

//获得最后一个,也就是默认为最大的一个钱币类型

int lastCoin = coinType[coinType.length-1];

//通过数组复制获得下一级调用时候的硬币类型

int[] newCoinType = new int[coinType.length-1];

System.arraycopy(coinType, 0, newCoinType, 0, newCoinType.length);

//获得针对当前硬币,也就是这个lastCoin 有几种找钱情况

int times = totalMoney/lastCoin;

ListString resultList = new ArrayListString();

//如果当前硬币的面值大于余额。但是手中还有更小面值的硬币,则递归调用找钱。

if(times==0coinType.length!=0){

ListString childList = reCoins(newCoinType, totalMoney);

resultList.addAll(childList);

//进行遍历,得到带找钱的值

}else{

//最小面值了,所以,直接返回值

if(coinType.length==1){

resultList.add(" 1分*"+totalMoney+"个 ");

}else{

//不是最小面值,所以,要遍历下

for(int i=1;i=times;i++){

ListString childList = null;

int remainder = totalMoney-lastCoin*i;//新余额

if(remainder != 0){

childList = reCoins(newCoinType, remainder);

for(String r:childList){

r = " "+lastCoin+"分*"+i+"个 " + r;

resultList.add(r);

}

}else{

resultList.add(" "+lastCoin+"分*"+i+"个 ");

}

}

}

}

return resultList;

}

}

加油,java的路 还有很长哦,呵呵

关于java找钱和java找钱问题递归的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-12-11,除非注明,否则均为首码项目网原创文章,转载请注明出处。