250x250
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 재귀함수
- BFS
- 주사위굴리기2
- 중복조합
- 볼륨 만들기
- 완전탐색
- 에라토스테네스의채
- 백준2251
- 파티션 크기 조정
- 순열
- 코테
- 백준15652
- 백준
- 중복순열
- 자바 코테
- 자바
- D드라이브생성
- 삼성역테
- 정올 1620
- 알고리즘개념
- N과M
- 코테준비
- Bfs와DFS
- 백준13458
- 전화번호속의암호
- java
- 정보처리기사
- 알고리즘
- 완탐
- 23288
Archives
- Today
- Total
뚱땅뚱땅
[문제] 백준 2869 달팽이는 올라가고 싶다 본문
728x90
* 백준 단계별로풀어보기- 기본수학 1 편
* 출처: www.acmicpc.net/problem/2869
2869번: 달팽이는 올라가고 싶다
첫째 줄에 세 정수 A, B, V가 공백으로 구분되어서 주어진다. (1 ≤ B < A ≤ V ≤ 1,000,000,000)
www.acmicpc.net
엄청 쉽다고 생각했는데, 이 문제의 핵심은 시간 초과였다.
원래 풀려고 했던 방법은 아래와 같지만, 시간초과이다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(in.readLine());
int A = Integer.parseInt(st.nextToken());
int B = Integer.parseInt(st.nextToken());
int V = Integer.parseInt(st.nextToken());
int day = 0;
int sum = 0;
while(sum<V) {
sum = sum+ A;
day++;
if(sum>=V) break;
sum = sum - B;
}
System.out.println(day);
}
}
그래서 풀이 법을 참고 했다.
정상에 올라가는 순간, 달팽이는 미끄러지지 않으므로 낮에 도착한다는 것을 알 수 있다.
즉, 낮을 x라고 한다면 밤은 x-1이다.
A * x - B( x-1) = V 이므로 x = (V-B) / (A-B)임을 알 수 있다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(in.readLine());
int A = Integer.parseInt(st.nextToken());
int B = Integer.parseInt(st.nextToken());
int V = Integer.parseInt(st.nextToken());
int day = (V-B)/(A-B);
if((V-B) % (A-B) != 0) day++;
System.out.println(day);
}
}
728x90
'알고리즘 > 백준' 카테고리의 다른 글
[문제] 백준 10757번 큰 수 A+B (0) | 2021.01.25 |
---|---|
[문제] 백준 10250번 ACM 호텔 (0) | 2021.01.25 |
[문제] 백준 1193번 분수찾기 (0) | 2021.01.24 |
[문제] 백준 2292번 벌집 (0) | 2021.01.24 |
[문제] 백준 1316번 그룹 단어 체커 (0) | 2021.01.20 |
Comments