[BaekJoon] #4796 - 캠핑 [Java][C++][Python]
[BaekJoon] #4796 - 캠핑 [Java][C++][Python]
1. 아이디어
연속하는 $P$일 중 $L$일만 쓸 수 있는 캠핑장을 $V$일 휴가 동안 최대 며칠 쓸 수 있는지 구하는 문제다. 쓸 수 있는 날이 $P$일 주기마다 앞 $L$일이라고 하면 어느 연속한 $P$일에도 쓸 수 있는 날이 정확히 $L$일이므로, 휴가의 앞쪽 $\lfloor V / P \rfloor \times P$일에서는 시작 위치와 상관없이 $\lfloor V / P \rfloor \times L$일을 쓴다. 남은 $V \bmod P$일에서는 쓸 수 있는 날이 많아야 $L$일이고 휴가 첫날이 주기의 첫날과 겹치면 $\min(V \bmod P, L)$일을 쓸 수 있으므로, 답은 $\lfloor V / P \rfloor \times L + \min(V \bmod P, L)$이다. 나머지를 그대로 더하지 않고 $L$과 비교해야 하며, $L < P$라 이 값은 $V$를 넘지 않아 입력이 32비트 정수 범위여도 넘치지 않는다.
2. 코드
풀이 [Java][C++][Python]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
for (int tc = 1; ; tc++) {
StringTokenizer st = new StringTokenizer(br.readLine());
int l = Integer.parseInt(st.nextToken());
int p = Integer.parseInt(st.nextToken());
int v = Integer.parseInt(st.nextToken());
if (l == 0) break;
sb.append("Case ").append(tc).append(": ").append(v / p * l + Math.min(v % p, l)).append("\n");
}
System.out.println(sb);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
for (int tc = 1;; tc++) {
int l, p, v;
cin >> l >> p >> v;
if (l == 0) break;
cout << "Case " << tc << ": " << v / p * l + min(v % p, l) << '\n';
}
}
1
2
3
4
5
6
7
8
9
10
11
import sys
input = sys.stdin.readline
tc = 1
while True:
l, p, v = map(int, input().split())
if l == 0:
break
print(f"Case {tc}: {v // p * l + min(v % p, l)}")
tc += 1
This post is licensed under CC BY 4.0 by the author.