프로그래밍 언어/코틀린(Kotline)

[Kotlin] Day 2: Opterators (HackerRank - 30 Days of Code)

나아가는중 2023. 3. 6. 23:14
반응형

Objective

In this challenge, you will work with arithmetic operators. Check out the Tutorial tab for learning materials and an instructional video.

 

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. Round the result to the nearest integer.

 

Example

A tip of 15% * 100 = 15, and the taxes are 8% * 100 = 8. Print the value  and return from the function.

 

Function Description

Complete the solve function in the editor below.

solve has the following parameters:

  • int meal_cost: the cost of food before tip and tax
  • int tip_percent: the tip percentage
  • int tax_percent: the tax percentage

Returns The function returns nothing. Print the calculated value, rounded to the nearest integer.

 

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).

 

Sample Input

12.00
20
8

Sample Output

15
 

 

문제풀이

팁과 세금을 음식값과 곱해준다음 퍼센트 계산을 해야 하므로 100으로 나눠줍니다.

 

결과를 가장 가까운 정수로 반환하기 위해 rountToInt()을 사용해줍니다.

소스코드

import kotlin.math.roundToInt

fun solve(meal_cost: Double, tip_percent: Int, tax_percent: Int): Unit {
    val tip = tip_percent * meal_cost / 100
    val tax = tax_percent * meal_cost / 100

    println((tip + tax + meal_cost).roundToInt())
}

fun main(args: Array<String>) {
    val meal_cost = readLine()!!.trim().toDouble()

    val tip_percent = readLine()!!.trim().toInt()

    val tax_percent = readLine()!!.trim().toInt()

    solve(meal_cost, tip_percent, tax_percent)
}
반응형