매일 Algorithm

신재원·2023년 4월 20일
1

Algorithm

목록 보기
102/243

백준 10801번 (Bronze 2)

import java.util.Scanner;

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

        int[] A = new int[10]; // A배열 선언
        int[] B = new int[10]; // B배열 선언

        for (int i = 0; i < 10; i++) {
            A[i] = in.nextInt();
        }
        for (int i = 0; i < 10; i++) {
            B[i] = in.nextInt();
        }

        int aCount = 0;
        int bCount = 0;
        for (int i = 0; i < 10; i++) {
            if (A[i] > B[i]) {
                aCount++;
            } else if (A[i] < B[i]) {
                bCount++;
            } else {
                continue;
            }
        }

        // 게임의 승패에 따른 출력
        if (aCount > bCount) {
            System.out.println("A");
        } else if (aCount < bCount) {
            System.out.println("B");
        } else {
            System.out.println("D");
        }
    }
}

백준 10804번 (Bronze 2)

import java.util.Scanner;

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

        int[] arr = new int[21]; // 배열 초기화

        for (int i = 1; i < 21; i++) {
            arr[i] = i; // arr 배열에 값 대입
        }

        for (int i = 0; i < 10; i++) {
            int a = in.nextInt();
            int b = in.nextInt();

            int mid = (b - a) / 2;

            // 조건을 '='로 한 이유는 예시로 5 ~ 10의 값을 
            // 바꿔야할 경우, 5 6 7 -> 8 9 10 으로 변경해야됨으로
            for (int j = 0; j <= mid; j++) {
                int temp = arr[a + j]; // 배열의 값 순서대로 임시 저장

                // 배열을 역순으로 저장
                arr[a + j] = arr[b - j];
                arr[b - j] = temp;
            }
        }

        for (int i = 1; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}

백준 10813번 (Bronze 2)

import java.util.Scanner;

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

        int n = in.nextInt();
        int m = in.nextInt();


        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = i + 1; // arr배열에 값 담기
        }


        for (int i = 0; i < m; i++) {
            int a = in.nextInt();
            int b = in.nextInt();

            // temp라는 변수에 값을 임시로 담아준다.
            int temp = arr[a - 1];
            arr[a - 1] = arr[b - 1];
            arr[b - 1] = temp;
        }

        for (int result : arr) {
            System.out.print(result + " ");
        }
    }
}

백준 10820번 (Bronze 2)

import java.util.Scanner;

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


        // 다음 입력이 없을떄 까지
        while (in.hasNextLine()) {
            String str = in.nextLine();
            
            // 소문자, 대문자, 숫자, 공백의 수를 담을 배열
            int[] result = new int[4]; 

            for (int i = 0; i < str.length(); i++) {
                char ch = str.charAt(i);
                // 소문자 검증
                if (ch >= 'a' && ch <= 'z') {
                    result[0]++;
                }
                // 대문자 검증
                if (ch >= 'A' && ch <= 'Z') {
                    result[1]++;
                }
                // 숫자 검증
                if (ch >= '0' && ch <= '9') {
                    result[2]++;
                }
                // 공백 검증
                if (ch == ' ') {
                    result[3]++;
                }
            }
            for (int r : result) {
                System.out.print(r + " ");
            }
            System.out.println();
        }
    }
}

백준 10821번 (Bronze 2)

import java.util.Scanner;

public class problem327 {
    public static void main(String[] args) {
        // 간단한 문자열 문제다.
        Scanner in = new Scanner(System.in);
        String[] str = in.next().split(",");
        System.out.print(str.length);
    }
}

백준 10822번 (Bronze 2)

import java.util.Scanner;

public class problem328 {
    public static void main(String[] args) {
        // 간단한 문자열 문제다.
        Scanner in = new Scanner(System.in);
        String[] str = in.next().split(",");
        int sum = 0;

        for(String s : str){
            // 문자열을 int로 변환후 sum에 값을 누적해준다.
            sum += Integer.parseInt(s);
        }
        System.out.println(sum);
    }
}

백준 10829번 (Bronze 2)

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

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

        long number = in.nextLong();

        List<Long> list = new ArrayList<>();

        while (number > 0) {
            long temp = number % 2;
            number /= 2;

            list.add(temp);
        }
        Collections.reverse(list);

        for (long i : list) {
            System.out.print(i);
        }
    }
}

0개의 댓글