[SWEA] 화섭이의 미생물 배양

문제 보러가기

제한사항

이외의 제한사항은 없다.

첫번째 생각

문제에서 시키는대로 하면 쉽게 풀릴 것이라고 생각했다.

두번째 생각

b가 1일때 예외가 발생해서 예외처리를 해주었다.

Code

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.io.IOException;
import java.util.Scanner;

public class Solution {
    public static void main(String[] args) throws IOException{
    	Scanner sc= new Scanner(System.in);
    	int T = sc.nextInt();
        for (int tc = 1; tc <= T; tc++) {
            int s = sc.nextInt();
            int t = sc.nextInt();
            int a = sc.nextInt();
            int b = sc.nextInt();
            if(b == 1) {
                int tmp = t-s;
                if(tmp%a==0) {
                    System.out.println("#"+tc+" "+(t-s)/a);
                }else {
                    System.out.println("#"+tc+" -1");
                }
                continue;
            }
            int goHome = 0;
            while(s!=t) {
                if(t%b==0) {
                    if(t/b < s) {
                        goHome++;
                        t -= a;
                    }else {
                        goHome++;
                        t /= b;
                    }
                }else {
                    goHome++;
                    t -= a;
                }
                if(s > t) {
                    goHome = -1;
                    break;
                }
            }
            System.out.println("#"+tc+" "+goHome);
        }
    }

}