매일 Algorithm

신재원·2023년 3월 29일
0

Algorithm

목록 보기
80/243

백준 1929번 (수학)

import java.util.Scanner;

public class problem252 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        int[] arr = new int[m + 1];

        for (int i = 2; i <= m; i++) {
            arr[i] = i; // 배열에 1부터 m까지 값을 초기화해준다.
        }

        // 제곱근 까지만 검증
        for (int i = 2; i <= Math.sqrt(m); i++) {
            if (arr[i] == 0) continue;
            // i의 배수를 0으로 바꿔준다.
            for (int j = i + i; j <= m; j += i) {
                arr[j] = 0;
            }
        }

        // 문제에서 n 부터 시작해서 m까지의 소수 갯수
        for (int i = n; i <= m; i++) {
            if (arr[i] != 0) {
                System.out.println(arr[i]);
            }
        }
    }
}

백준 3009번 (구현)

import java.util.Scanner;

public class problem253 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int [] x = new int[3];
        int [] y = new int[3];

        for(int i = 0; i < 3; i++){
            x[i] = in.nextInt();
            y[i] = in.nextInt();
        }
        // x 좌표, y 좌표 한개만 입력된 좌표를 출력해준다.

        int nowX, nowY;
        if(x[0] == x[1]){
            nowX = x[2];
        }else if(x[0] == x[2]){
            nowX = x[1];
        }else{
            nowX = x[0];
        }

        if(y[0] == y[1]){
            nowY = y[2];
        }else if(y[0] == y[2]){
            nowY = y[1];
        }else{
            nowY = y[0];
        }
        System.out.print(nowX + " " + nowY);
    }
}

0개의 댓글