URI Problem (Banknotes and Coins) 1021 Solution in Java

Problem Solving, URI

URI Problem (Banknotes and Coins) 1021:
Banknotes and Coins is a basic problem on URI online judge for novice problem solver.
You can find details on this Link.

import java.text.DecimalFormat;
import java.util.Scanner;

/**
 * @Author : Muhammad Harun-Or-Roshid
 * @Date : Oct 9, 2016
 * @Time : 5:42:42 PM
 */
public class Uri1021 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        double n = sc.nextDouble();
        if (n >= 0 && n <= 1000000.00) {
            int notes[] = {100, 50, 20, 10, 5, 2};
            double coins[] = {1.00, 0.50, 0.25, 0.10, 0.05, 0.01};
            System.out.println("NOTAS:");
            for (int i = 0; i < notes.length; i++) {
                int t = (int) (n / notes[i]);
                System.out.printf("%d nota(s) de R$ %.2f\n",t ,  (double)notes[i]);             
                n = toDouble(n - notes[i]*t);
            }
            System.out.println("MOEDAS:");
            for (int i = 0; i < coins.length; i++) {
                int t = (int) (n / coins[i]);
                System.out.printf("%d moeda(s) de R$ %.2f\n", (int) (n / coins[i]), coins[i]);
                n = toDouble(n - coins[i]*t);
            }
        }
        
    }
    private static double toDouble(double x){
            DecimalFormat format = new DecimalFormat("#0.00");
            return Double.valueOf(format.format(x));
        }
}

0 Comments

You may find interest following article

Chapter 4 Relational Algebra

Relational Algebra The part of mathematics in which letters and other general symbols are used to represent numbers and quantities in formula and equations. Ex: (x + y) · z = (x · z) + (y · z). The main application of relational algebra is providing a theoretical foundation for relational databases, particularly query languages for such databases. Relational algebra...

Chapter 3 Components of the Database System Environment

Components of the Database System Environment There are five major components in the database system environment and their interrelationships are. Hardware Software Data Users Procedures Hardware:  The hardware is the actual computer system used for keeping and accessing the database. Conventional DBMS hardware consists of secondary storage devices, usually...