<30계단> coci_slatkisi(사탕 먹기)

2015. 10. 24. 14:42Algorithm Solution


알고리즘 문제 


  프로그램 명: coci_slatkisi

제한시간: 1 초

미르코는 사탕가게안에 있는 매우 많은 사탕을 샀다. 그는 항상 정확한 양을 지불하지 못해서 가게주인 아주머니와 협상을 했다. 미르코는 아주머니에게 본인이 가진 가장 작은 지폐를 말하고, 그녀는 그가 지불할 수 있는 가장 가까운 값을 "반올림"한다.

예를 들어보자. 만약 미르코가 최소 100쿠나 지폐를 가지고 있고 그가 150쿠나 어치의 사탕 사길 원한다면, 가게 주인은 200쿠나로 반올림할 것이다. 만약 그가 149쿠나 만큼 사길 원하면, 그녀는 100쿠나로 반올림할 것이다.

최근 미르코는 가게 주인이 그를 속이고 있다고 의심한다. 그는 당신에게 그를 도와줄 수 있는 프로그램을 작성하길 원한다.

그녀의 어머니는 1, 10, 100, ... , 1000000000 단위의 지폐만을 준다. 그는 반드시 10의 거듭제곱꼴의 지폐만을 가진다. 미르코는 지폐를 매우 많이 가지고 있다.

입력

첫번째 줄에는 사탕의 가격 C (0 ≤ C ≤ 1 000 000 000)와 미르코가 가진 최소 지폐의 단위 K가 주어진다. (10^K 이 최소 지폐이다.)

출력

미르코가 지불해야하는 반올림한 값을 출력한다.

입출력 보충

입력

184 1

출력

180

10^1 = 10 이 최소로 가진 단위이니 180 190 중 180 이 가까워서 답은 180 

입력

182 2

출력

200

10^2= 100 이 최소로 가진 단위이니 100 과 200 중 200 이 가까워서  200

Mirko buys a lot of candy in the candy shop. He cannot always pay the exact ammount so the shopkeeper and he have an agreement. He tells the shopkeeper the smallest bill he has, and she rounds his ammount to the nearest number he can pay.

For example, if the smallest bill Mirko has is a hundred bill, and he wants to buy 150 Kunas of candy, the shopkeeper rounds his ammount to 200 Kunas. If he wants to buy 149 Kunas of candy, the shopkeeper rounds his ammount to 100 Kunas.

Lately, Mirko suspects the shoopkeeper is trying to cheat him. He asked you to help him. Write a program that will help him.

His mother only gives Mirko 1, 10, 100, 1 000, ... , 1 000 000 000 Kuna bills. He never has bills that are not powers of 10. The bills he does have, he has in large ammounts. Mirko buys a lot of candy in the candy shop. He cannot always pay the exact ammount so the shopkeeper and he have an agreement. He tells the shopkeeper the smallest bill he has, and she rounds his ammount to the nearest number he can pay.

For example, if the smallest bill Mirko has is a hundred bill, and he wants to buy 150 Kunas of candy, the shopkeeper rounds his ammount to 200 Kunas. If he wants to buy 149 Kunas of candy, the shopkeeper rounds his ammount to 100 Kunas.

Lately, Mirko suspects the shoopkeeper is trying to cheat him. He asked you to help him. Write a program that will help him.

His mother only gives Mirko 1, 10, 100, 1 000, ... , 1 000 000 000 Kuna bills. He never has bills that are not powers of 10. The bills he does have, he has in large ammounts.

입력

The first and only line of input contains two integers, C (0 ≤ C ≤ 1 000 000 000), the price of candy Mirko is going to buy, and K (0 ≤ K ≤ 9), number of zeros on the smallest bill Mirko has.

출력

The first and only line of output should contain one integer, C rounded to the nearest amount Mirko can pay.

입출력 예

입력

184 1

출력

180

입력

123450995 1

출력

123451000

입력

182 2

출력

200
출처: COCI 2009/2010 contest3 2/6
번역: Fate






처음에 이걸 푸느라 시간이 많이 걸렸다. 이 문제는 한마디로 반올림 하는거다. 


1

2

3

4

예를 들면 1234 숫자가 있다. 여기서 1234 2 입력을 하면 10^2 이기 때문에 100 의미한다. 그러면 답은 십의 자리인 3 보고 5미만인 이기 때문에 1200으로 답이 나와야 한다따라서 입력 번째 값인 2  십의 자릿수인 3 구하고서 반올림 할지 할지 판단을 하면 된다. 그러면 3 어떻게 구할까? 

생각으로는 1234 에서 10^2 나눈 나머지를 구한다. 그리고 나머지를 10^1 값으로 나눈 몫이 바로 내가 구하고자 3 값이다. 간단하게 다시 한번 설명하겠습니다. 



1

2

3

4

입력 : 1234 2(K)
결과 : 1200

1234 / 100 = 12
나머지 34이다.

34/10 = 3 나머지 4 나온다.


K
값을 하나를 줄여서 나눈 몫이 바로 구하고자 3 나온다


소스 코드는 다음과 같습니다. 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.util.Scanner;
 
public class Main {
 
    public static void main(String[] args) {
 
        Scanner sc = new Scanner(System.in);
        int candy = sc.nextInt(); //사탕의 값 
        int k = sc.nextInt(); // 화폐의 최소 단위 지수의 값
        int k_num = (int) Math.pow(10, k); //10 의 K제곱 값
        int result = 0//우리가 구하고자 한 최종 값 
        int standard = candy % k_num; //기준을 저장 하는 값 
        int j = k;
        for (int i = 0; i < 2; i++) {
 
            if (standard < 5) {
                result = (candy / k_num) * k_num;
            } else if (standard >= 5) {
                result = ((candy / k_num) * k_num) + k_num;
            }
 
            if (k_num != 10) {
                standard = standard / (int) Math.pow(10--j);
            }
            
        }
        System.out.println(result);
 
    }
}
 
 
cs

 

 




'Algorithm Solution' 카테고리의 다른 글

백준 1152번 단어의 갯수 세기 문제<java>  (0) 2016.12.07
Prime Number(소수) 구하기 java  (0) 2016.01.13
피보나치 수열 구하기  (0) 2016.01.10
<30계단>angle(open)  (0) 2015.10.24
<30계단> maxandmin  (0) 2015.10.24