ddubi

백준 2869 : 달팽이는 올라가고 싶다 java 본문

코테 문제풀이

백준 2869 : 달팽이는 올라가고 싶다 java

ddubi__ 2022. 11. 9. 12:39

링크

https://www.acmicpc.net/problem/2869

 

2869번: 달팽이는 올라가고 싶다

첫째 줄에 세 정수 A, B, V가 공백으로 구분되어서 주어진다. (1 ≤ B < A ≤ V ≤ 1,000,000,000)

www.acmicpc.net

특징

시간 제한 0.15 초 (추가 시간 없음) 

 

풀이

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class back_2869 { // 달팽이는 올라가고 싶다

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] data = br.readLine().split(" ");
		
		int A = Integer.valueOf(data[0]);
		int B = Integer.valueOf(data[1]);
		int V = Integer.valueOf(data[2]);
		
		int day = (int) Math.ceil((V-B)/(A-B));
		// ceil 올림
		// floor 내림
		// round 반올림
		System.out.println(day);
		
	}

}

 

+ 틀린풀이(1) --> while

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		int up = sc.nextInt();
		int down = sc.nextInt();
		int goal = sc.nextInt();
		int snail = 0;

		int day = 0;
		while (snail < goal) {
			day++;
			snail += up;
			if (snail >= goal) {
				break;
			}
			snail -= down;
		}
		System.out.println(day);
	}

}

+ 틀린풀이(2) --> Scanner

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		double A = sc.nextInt();
		double B = sc.nextInt();
		double V = sc.nextInt();

		double answer = (V - B) / (A - B);
		if (answer % 1 != 0) {
			answer++;
		}
		System.out.println((int) answer);
	}
}

 

시간 제한이 있는 문제

Scanner, while을 사용하면 시간 초과가 되어버리기 때문에 주의!

Comments