백준 온라인 코딩 문제풀이
Baekjoon Online Judge
Baekjoon Online Judge 프로그래밍 문제를 풀고 온라인으로 채점받을 수 있는 곳입니다.
www.acmicpc.net
코드 참고
wny0320 (박 상운)
Run code live in your browser. Write and run code in 50+ languages online with Replit, a powerful IDE, compiler, & interpreter.
replit.com
2839번에 대해 다룸
2839
using System;
class Program
{
static void Main(string[] args)
{
int input = int.Parse(Console.ReadLine());
int i = 0;
int buff = input;
int a = 0;
int b = 0;
while(input != 0)
{
input = buff;
a = input / 5;
if (a - i > 0)
{
input = input - 5 * (input/5 - i);
}
b = input / 3;
input = input - 3 * (input/3);
if(a-i < 0 && i > 0)
{
i = -1;
break;
}
i++;
}
if(i == -1)
{
Console.WriteLine(-1);
}
else
{
i -= 1;
Console.WriteLine(a - i + b);
}
}
}
코드가 쓰다보니 조금 난잡해짐
원래는 buff값으로 while문 안에서 계산해야하지만 중간에 추가해서 input으로 계산을 함
input / 5한 몫만큼 5를 곱해서 input 값에 빼고 그 값에 input / 3한 몫만큼 3을 곱해서 input값에 뺏을 때
0이 된다면 그것이 최소 값이라고 생각했음
이제 안되는 경우 i값을 증가시켜 input / 5의 몫을 하나씩 줄여 빼서 계산해보고
그렇게 해서 input / 5 - i 값이 0이 되었을 때가 input을 3으로만 나눈 마지막 계산이 됨
따라서 input / 5 - i가 음수가 되면 답이 존재하지 않으므로 -1이 됨
'BaekJoon' 카테고리의 다른 글
기본 수학 2단계 1978 (0) | 2022.10.28 |
---|---|
기본 수학 1단계 10757 (0) | 2022.10.21 |
기본 수학 1단계 10250 (0) | 2022.10.12 |
기본 수학 1단계 2869 (0) | 2022.10.10 |
기본 수학 1단계 2292 (0) | 2022.10.06 |