본문 바로가기
ETC

코딩 테스트

by newny 2023. 10. 3.
반응형

1. 백준 2018

[풀이]

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int numN = Integer.parseInt(sc.nextLine());
        int sum = 1;
        int start = 1;
        int end = 1;
        int count = 1;

        while (Math.ceil(numN / 2.0) > start) {
            if (sum == numN) {
                count++;
                end++;
                sum += end;
            } else if (sum > numN) {
                sum -= start;
                start++;
            } else {
                end++;
                sum += end;
            }
        }
        System.out.println(count);
    }
}

2. 백준 1940

[풀이]

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.Arrays;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine());
        int M = Integer.parseInt(br.readLine());

        StringTokenizer st = new StringTokenizer(br.readLine());
        int[] numbers = new int[N];
        for (int i = 0; i < N; i++) {
            numbers[i] = Integer.parseInt(st.nextToken());
        }
        Arrays.sort(numbers);

        int start = 0;
        int end = N-1;
        int count = 0;

        while (start < end) {
            if (numbers[start] + numbers[end] == M) {
                count++;
                start++;
                end--;
            } else if (numbers[start] + numbers[end] < M) {
                start++;
            } else {
                end--;
            }
        }
        System.out.println(count);
    }
}
반응형

'ETC' 카테고리의 다른 글

코딩테스트  (0) 2023.10.13
코딩 테스트  (1) 2023.10.02
crawling 실습  (0) 2023.03.29

댓글