본문 바로가기

Algorithms/30 Days of Code

Day 2: Operators

Task 
Given the meal price (base cost of a meal), tip percent (the percentage of the meal price being added as tip), and tax percent (the percentage of the meal price being added as tax) for a meal, find and print the meal's total cost.

Note: Be sure to use precise values for your calculations, or you may end up with an incorrectly rounded result!

Input Format

There are  lines of numeric input: 
The first line has a double,  (the cost of the meal before tax and tip). 
The second line has an integer,  (the percentage of  being added as tip). 
The third line has an integer,  (the percentage of  being added as tax).

Output Format

Print The total meal cost is totalCost dollars., where  is the rounded integer result of the entire bill ( with added tax and tip).

Sample Input

12.00
20
8

Sample Output

The total meal cost is 15 dollars.

Explanation

Given: 

Calculations: 
 
 
 

We round  to the nearest dollar (integer) and then print our result:

The total meal cost is 15 dollars.


풀이


import java.io.*;

import java.util.*;

import java.text.*;

import java.math.*;

import java.util.regex.*;


public class Solution {


    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        double meal_cost = in.nextDouble();

        int tip_percent = in.nextInt();

        int tax_percent = in.nextInt();

        in.close();

        int total = calculateTotalCost(meal_cost, tip_percent, tax_percent);

        System.out.println("The total meal cost is " + total +" dollars.");

    }

    

    private static Integer calculateTotalCost(double mealCost, int tipPercent, int taxPercent){

        double tip = mealCost * tipPercent/100;

        double tax = mealCost * taxPercent/100;

        int total = (int)Math.round(mealCost + tip + tax);

        return total;

    }

}

'Algorithms > 30 Days of Code' 카테고리의 다른 글

Day 5: Loops  (0) 2018.02.13
Day 4: Class vs. Instance  (0) 2018.02.13
Day 3: Intro to Conditional Statements  (0) 2018.02.12
Day 1: Data Types  (0) 2018.02.09
Day 0: Hello, World.  (0) 2018.02.08