Java-the best combination of change for each customer after payment

Source: Internet
Author: User

Suppose the price of a store item (R) is not less than (and is an integer), if the customer pays (P) within the million, The existing program can be paid after each customer to give the best combination of change (to find the minimum number of customers currency). Assume that the currency value of this store includes only: $ (N50), Yuan (N10), 5 Yuan (N5),1 yuan (N1) four kinds.

Here is the code for the program:

/************************************************************   copyright  (C),  1988-1999, huawei tech. co., ltd.   filename: test.cpp    Author:  Light      Version : 1.0      Date:   2018/4/17Description:      Suppose the price of a store's goods (R) is not more than 100 yuan (and is an integer), if the customer payment (P) within 100 yuan, the existing program can be paid after each customer to give the best combination of change (to find the minimum number of customers currency). Assume that the currency value of this store includes only: 50 Yuan (N50), 10 yuan (N10), 5 yuan (N5), 1 yuan (N1) Four//  module description           Version:  for software testing only, complete job//  version information    Function List: //  main functions and their functions       1. History:   //  History Modification Records         <author>  <time>   <version >   <desc >        light    18/4/17     1.0      establishing a function  ****************************** /package Demo;import Java.util.scanner;public class Main {/** * main function * @param args */public St atic void Main (string[] args) {//TODO auto-generated method Stubint pay = 0;//payment amount int cost = 0;//commodity spends int change = 0;//Money @SuppressWarnings ("resource") Scanner Scanner = new Scanner (system.in); System.out.println ("Please enter customer payment amount:");//detection of input content, compliance with rules Try{pay = Scanner.nextint (); if (Pay > | | Pay < 0) { System.out.println ("Please enter the correct amount"); return;}} catch (Exception e) {System.out.println ("Please enter a valid number"); return;} System.out.println ("Please enter store Item Price:");//Check the input content, whether it conforms to the rules Try{cost = Scanner.nextint (); if (Cost > | | Cost < 0) { System.out.println ("Please enter the correct amount"); return;} else if (Cost > Pay) {System.out.println ("Your spending exceeds the amount paid"); return;}} catch (Exception e) {System.out.println ("Please enter a valid number"); return;} Change = pay-cost;//output at this time of payment amount, commodity price, should find amount System.out.println ("Payment amount" + Pay + "Item Price" + Cost + "should find Total Amount:" + change);//Get result, output string end = Givechange (change); System.out.println ("The consumption should find the amount" + Change + "yuan, which should find" + end);} /** * Change the change rule * @param change * @return string//return the final change result */public static String givechange (int change) {string str= ""; int qianshu;//should change the number int leftmoney = change;//How much money is left, the initial value is Changeif (leftmoney/50! = 0) {Qianshu = Leftmoney/50;str = str + q Ianshu + "Zhang 50 yuan"; Leftmoney = Leftmoney-qianshu * 50;} if (LEFTMONEY/10! = 0) {Qianshu = Leftmoney/10;str = str + Qianshu + "$ 10"; Leftmoney = Leftmoney-qianshu * 10;} if (LEFTMONEY/5! = 0) {Qianshu = Leftmoney/5;str = str + Qianshu + "$ 5"; Leftmoney = Leftmoney-qianshu * 5;} if (LEFTMONEY/1! = 0) {Qianshu = Leftmoney/1;str = str + Qianshu + "$ 1"; Leftmoney = Leftmoney-qianshu * 1;} Check whether to complete System.out.println ("The remainder of the money is not found:" +leftmoney); return str;}

  

Java-the best combination of change for each customer after payment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.