Baekjoon - 2869

Tadap·2023년 9월 15일
0

Baekjoon

목록 보기
16/94

문제

Solved.ac Class2++

1차시도

public class Main {
	public static void main(String[] args) throws Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] split = br.readLine().split(" ");
		int a = Integer.parseInt(split[0]);
		int b = Integer.parseInt(split[1]);
		int v = Integer.parseInt(split[2]);

		System.out.println(solve(a, b, v));

	}

	private static int solve(int a, int b, int v) {
		int sum = 0;
		int day = 0;
		while (sum < v) {
			if (sum + a >= v) {
				return day + 1;
			}
			sum += a;
			sum -= b;
			day++;
		}
		return day;
	}
}

시간초과

2차시도

public class Main {
	public static void main(String[] args) throws Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] split = br.readLine().split(" ");
		int a = Integer.parseInt(split[0]);
		int b = Integer.parseInt(split[1]);
		int v = Integer.parseInt(split[2]);

		System.out.println(solve(a, b, v));

	}

	private static int solve(int a, int b, int v) {
		int target = v - a;
		int day = target / (a - b);
		if (target % (b - a) > 0) {
			day++;
		}

		return day + 1;
	}
}

확인해보니 시간이 0.25초로 짧게 잡혀있다.
다른 방법이 있다는 뜻 따라서 규칙을 찾았다

  1. 먼저 v미터에 도달하기 전 날을 잡았다 이를 위해 v-a를 해서 target을 잡았다.
  2. 이후 target까지는 미끄러지는거 포함해서 올라갈 것이므로 b-a로 나눠 나머지 값을 더해 주었다
    • 이때 나머지가 있다는 말은 아직 target보다 아래라는 말이므로 day에 값을 더해주었다
  3. 이후 +1을 해 도달하는 날까지 포함하여 주었다

성공

0개의 댓글