매일 Algorithm

신재원·2023년 4월 5일
0

Algorithm

목록 보기
87/243

백준 11718번 (구현)

import java.util.Scanner;

public class problem276 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        // hasNextLine : 다음 입력값이 있는지 확인, boolean 타입으로 반환
        while(in.hasNextLine()){

            String str = in.nextLine();
            System.out.println(str);
        }
    }
}

백준 1551번 (Bronze 1)

import java.util.Scanner;

public class problem277 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int size = in.nextInt();
        int cut = in.nextInt();
        int[] arr = new int[size];

        String[] str = in.next().split(",");

        for (int i = 0; i < arr.length; i++) {
            arr[i] = Integer.parseInt(str[i]);
        }

        // 몇번 반복할지 for문
        for (int i = 0; i < cut; i++) {
            for (int j = 0; j < size - 1; j++) {
                arr[j] = arr[j + 1] - arr[j]; // k번 했을경우 arr 배열이 갱신
            }
        }

        for (int i = 0; i < size - cut; i++) {
            System.out.print(arr[i]);

            // 마지막값을 출력후 쉼표를 출력하지 않기위해
            if (i != size - cut - 1) {
                System.out.print(",");
            }
        }
    }
}

백준 2729번 (Bronze 1)

import java.math.BigInteger;
import java.util.Scanner;

public class problem278 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int size = in.nextInt();
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < size; i++) {
            String a = in.next();
            String b = in.next();

            // BigInteger : 큰 정수를 계산하기 위해, 2진수로 변환
            BigInteger n = new BigInteger(a, 2);
            BigInteger m = new BigInteger(b, 2);

            // StringBuilder에 2진수로 입력된 문자열을 정수로 변환후 append
            BigInteger total = n.add(m);

            // .toString(2) : 2진수로 변환
            sb.append(total.toString(2)).append("\n");
        }

        System.out.print(sb.toString());

    }
}

0개의 댓글