Published:
Updated:

평소 코딩 테스트는 파이썬으로 연습했지만, 자바로도 할 수 있도록 대비하기 위함입니다.


큰 수 출력하기 Solution

import java.util.*;

public class Main {

    public String solution(int N, int[] inputs) {
        StringBuilder answer = new StringBuilder();

        answer.append(inputs[0]).append(" ");
        for (int i = 1; i < inputs.length; i++) {
            if (inputs[i] > inputs[i - 1]) {
                answer.append(inputs[i]).append(" ");
            }
        }

        return answer.toString();
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int N = Integer.parseInt(in.nextLine());
        String[] split = in.nextLine().split(" ");
        int[] inputs = Arrays.stream(split)
                .mapToInt(Integer::parseInt)
                .toArray();

        System.out.println(new Main().solution(N, inputs));
    }
}


보이는 학생 Solution

import java.util.*;

public class Main {

    public int solution(int N, int[] heights) {
        int answer = 0;
        int currentMaxHeight = -1;

        for (int height : heights) {
            if (height > currentMaxHeight) {
                currentMaxHeight = height;
                answer += 1;
            }
        }

        return answer;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int N = Integer.parseInt(in.nextLine());
        int[] heights = Arrays.stream(in.nextLine().split(" "))
                .mapToInt(Integer::parseInt)
                .toArray();
        System.out.println(new Main().solution(N, heights));
    }
}


가위 바위 보 Solution

import java.util.*;

public class Main {

    public void solution(int N, int[] A, int[] B) {
        final String WINNER_A = "A";
        final String WINNER_B = "B";
        final String DRAW = "D";

        // 1: 가위, 2: 바위, 3: 보
        for (int i = 0; i < N; i++) {
            int currentA = A[i];
            int currentB = B[i];

            if (currentA == currentB) {
                System.out.println(DRAW);
                continue;
            }

            if (currentA == 1) {
                if (currentB == 2) {
                    System.out.println(WINNER_B);
                    continue;
                }

                System.out.println(WINNER_A);
                continue;
            }

            if (currentA == 2) {
                if (currentB == 1) {
                    System.out.println(WINNER_A);
                    continue;
                }

                System.out.println(WINNER_B);
            }

            if (currentA == 3) {
                if (currentB == 1) {
                    System.out.println(WINNER_B);
                    continue;
                }

                System.out.println(WINNER_A);
            }
        }
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int N = Integer.parseInt(in.nextLine());
        int[] A = Arrays.stream(in.nextLine().split(" "))
                .mapToInt(Integer::parseInt)
                .toArray();
        int[] B = Arrays.stream(in.nextLine().split(" "))
                .mapToInt(Integer::parseInt)
                .toArray();
        new Main().solution(N, A, B);
    }

}


피보나치 수열 Solution

import java.util.*;

public class Main {

    public int[] solution(int N) {
        int[] answer = new int[N];
        answer[0] = 1;
        answer[1] = 1;

        for (int i = 2; i < N; i++) {
            answer[i] = answer[i - 1] + answer[i - 2];
        }

        return answer;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int N = in.nextInt();
        for (int i : new Main().solution(N)) {
            System.out.print(i + " ");
        }
    }
}


소수(에라토스테네스 체) Solution

import java.util.*;

public class Main {

    public int solution(int N) {
        int answer = 0;

        // 0부터 N까지니까 N + 1개
        int[] prime = new int[N + 1];
        for (int i = 2; i <= N; i++) {
            if (prime[i] == 0) {
                answer++;

                for (int j = i; j <= N; j += i) {
                    prime[j] = 1;
                }
            }
        }

        return answer;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int N = in.nextInt();
        System.out.println(new Main().solution(N));
    }

}


Reference

Leave a comment