2909번 캔디 구매 | 뭐라도 하겠지

2909번 캔디 구매

이왜틀?


img01

2909번 캔디 구매 - 백준

빠른 번호의 낮은 레벨 문제는 테스트 케이스가 아주 많다는 것
어떤 반례의 함정에 빠져 틀렸을까?

StringTokenizer st=new StringTokenizer(br.readLine()," ");
long candy=Long.parseLong(st.nextToken());
long zero=Long.parseLong(st.nextToken());
long money=1;
for(long i=0;i<zero;i++) {
    money*=10;
}

long price=candy/money*money;

if(candy%money>=money/2) candy=price+money;
else candy=price;
bw.write(candy+"");

음 잘 모르겠다 나름 짱구 잘 굴린 것 같은데
얼추 맞지 않나?

설계


일단 파이썬으로 깔끔하게 다시 짜보자.
decimal을 사용해 반올림한다.

구현


from decimal import Decimal, ROUND_HALF_UP

C, K = map(int, input().split())

shift = Decimal('1e{}'.format(K))
result = (Decimal(C) / shift).to_integral_value(rounding=ROUND_HALF_UP) * shift

print(int(result))

채점


img02

반성


괜히 뻘짓하지말고 Math.round를 썼으면 통과했으려나?
큰 수에서 걸릴 일은 K가 9라서 없었을텐데
작은 수에서 걸렸나?

뭐 풀었으니 그냥 넘어가야지.

코드 확인


Link to GitHub

Updated: