매일 Algorithm

신재원·2023년 3월 26일
0

Algorithm

목록 보기
77/243

백준 2999번 (Bronze 1)

import java.util.Scanner;

public class problem241 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.next();
        char [] ch = str.toCharArray();
        int n = 0;
        int m = 0;
        int size = str.length();
        int sqrt = (int) Math.sqrt(size); // 제곱근으로 변경
        for(int i = 1; i <= sqrt; i++){
            if(size % i == 0){
                n = i;
                m = size / i;
            }
        }

        char [][] arr = new char [n][m];

        int index = 0;
        for(int i = 0; i < m ; i++){
            for(int j = 0; j < n; j++){
            // 입력된 문자열을 문자로 변환후, 열에 맞춰 문자를 담아준다.
                arr[j][i] = ch[index++]; 
            }
        }

        for(int i = 0; i < n; i++){
            for(int j = 0; j < m; j++){
                System.out.print(arr[i][j]);
            }
        }

    }

}

백준 3035번 (Bronze 1)

import java.util.Scanner;

public class problem242 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt(); // 열
        int m = in.nextInt(); // 행
        int u = in.nextInt(); // 확대할 열
        int p = in.nextInt(); // 확대할 행
        char[][] arr = new char[n][m];
        for (int i = 0; i < n; i++) {
            String str = in.next();
            for (int j = 0; j < m; j++) {
                arr[i][j] = str.charAt(j);
            }
        }


        for (int i = 0; i < n; i++) {
            for (int k = 0; k < u; k++) {
                for (int j = 0; j < m; j++) {
                    for (int q = 0; q < p; q++) {
                        // 확대한 정수 만큼 동일값을 출력해준다.
                        System.out.print(arr[i][j]);
                    }
                }
                // 한줄 끝나면 공백
                System.out.println();
            }
        }

    }
}

백준 11659번 (수학)

import java.util.Scanner;

public class problem243 {

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

        int a = in.nextInt();
        int b = in.nextInt();
        int[] arr = new int[a + 1];

        // 5 9 12 14 15
        for (int i = 1; i <= a; i++) {
            // 0번 인덱스는 값을 채워주지 않는다.
            arr[i] = arr[i - 1] + in.nextInt();

        }
        for (int j = 0; j < b; j++) {
            int n = in.nextInt();
            int m = in.nextInt();
            System.out.println(arr[m] - arr[n - 1]);
        }
    }
}

백준 2018번 (투 포인터)

import java.util.Scanner;

public class problem244 {

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

        int n = in.nextInt();
        int count = 1; // 자기 자신을 포함하는 1가지
        int start = 1;
        int end = 1;
        int sum = 1;
        // 1 2 3 4 5 6 7 8 9 10

        while (end != n) {
            if (sum == n) {
                count++;
                end++;
                sum += end;
            } else if (sum > n) {
                sum -= start;
                start++;

            } else {
                end++;
                sum += end;
            }
        }
        System.out.print(count);
    }
}

0개의 댓글