문제 출처: https://www.acmicpc.net/problem/6064



문제 풀이 참고 출처: http://mygumi.tistory.com/325


그냥 풀면 시간초과가 난다. 

x를 먼저 맞추고, y만을 신경쓴다.

그렇게 되면 y는 M만큼 증가하고, y를 N으로 나머지 연산을 하면, y의 값을 알 수 있다. 


마지막 해는 최소공배수이다. M=10, N=12일 때 10과 12의 최소공배수는 60이다.


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
46
import java.io.BufferedReader;
import java.io.InputStreamReader;
 
public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int T = Integer.parseInt(reader.readLine());
        StringBuilder builder = new StringBuilder();
        
        for(int n=; n<T ; n++){
            String[] input = reader.readLine().split(" ");
            int M = Integer.parseInt(input[0]);
            int N = Integer.parseInt(input[1]);
            int x = Integer.parseInt(input[2]);
            int y = Integer.parseInt(input[3]);
            int count = x, j = x, lcm = lcm(M, N); // x를 먼저 맞춘다. 
            
            for(int i=; i<lcm/M ; i++){
                int tempY = j % N == ? N : j % N; // y를 N으로 나머지 연산
                
                if (tempY == y)
                    break;
                
                count += M;
                j = tempY + M;
            }
            count = (count > lcm) ? -: count; // count가 마지막 해보다 크면 -1
        
            builder.append(count).append("\n");
        }
        
        System.out.println(builder);
    }
    
    public static int lcm(int num1, int num2) { // 최소공배수 구하기
        int a = num1, b = num2;
        
        while (b != 0) {
            int temp = a % b;
            a = b;
            b = temp;
        }
        
        return num1 * num2 / a;
    }
}
cs


'Algorithm' 카테고리의 다른 글

백준 1966번: 프린터 큐  (0) 2018.10.25
백준 10845번: 큐  (0) 2018.10.25
백준 1475번: 방 번호  (0) 2018.10.24
백준 2775번: 부녀회장이 될테야  (0) 2018.10.24
백준 1193번: 분수찾기  (0) 2018.10.24

문제 출처: https://www.acmicpc.net/problem/1475



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
import java.io.BufferedReader;
import java.io.InputStreamReader;
 
public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String N = reader.readLine();
        int[] number = new int[10];
        
        for(int i=; i<N.length() ; i++)
            number[N.charAt(i)-'0']++// 0-9 개수 카운트
        
        int result = 0;
        int sixNine = (number[6]+number[9]) / + (number[6]+number[9]) % 2// 6과 9 계산
        
        for(int i=; i<10 ; i++){
            if(i==|| i==9)
                continue;
            
            if(result < number[i])
                result = number[i];
        }
        
        if(result < sixNine)
            result = sixNine;
        
        System.out.println(result);
    }
}
cs


'Algorithm' 카테고리의 다른 글

백준 10845번: 큐  (0) 2018.10.25
백준 6064번: 카잉 달력  (0) 2018.10.24
백준 2775번: 부녀회장이 될테야  (0) 2018.10.24
백준 1193번: 분수찾기  (0) 2018.10.24
백준 2292번: 벌집  (0) 2018.10.24

문제 출처: https://www.acmicpc.net/problem/2775



규칙을 찾는다.

2층: 01 04 10 ...

1층: 01 03 06 10 15 21 28 36 45 55 66 78 91 105

0층: 01 02 03 04 05 06 07 08 09 10 11 12 13 14 

=> 2층의 3호 사람 수 (10) = 2층의 2호 사람 수 (4) + 1층의 3호 사람 수 (6)


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
import java.io.BufferedReader;
import java.io.InputStreamReader;
 
public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int T = Integer.parseInt(reader.readLine());
        StringBuilder builder = new StringBuilder();
        int[][] APT = new int[15][15];
        
        for(int i=; i<15 ; i++)
            APT[0][i] = i;
        
        for(int i=; i<15 ; i++)
            for(int j=; j<15 ; j++)
                APT[i][j] += APT[i][j-1+ APT[i-1][j];
        
        for(int i=; i<T ; i++){
            int k = Integer.parseInt(reader.readLine());
            int n = Integer.parseInt(reader.readLine());
            builder.append(APT[k][n]).append("\n");
        }
        
        System.out.println(builder);
    }
}
cs


'Algorithm' 카테고리의 다른 글

백준 6064번: 카잉 달력  (0) 2018.10.24
백준 1475번: 방 번호  (0) 2018.10.24
백준 1193번: 분수찾기  (0) 2018.10.24
백준 2292번: 벌집  (0) 2018.10.24
백준 9020번: 골드바흐의 추측  (0) 2018.10.21

+ Recent posts