매일 Algorithm

신재원·2023년 4월 23일
0

Algorithm

목록 보기
104/243

백준 2355번 (Bronze 2)

import java.util.Scanner;

public class problem335 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        long a = in.nextLong();
        long b = in.nextLong();

        if (a > b) { // a가 b보다 클 경우, 두 값을 스왑합니다.
            long temp = a;
            a = b;
            b = temp;
        }

        // 1 3 -> 6
        // 등차수열 합 공식을 이용하여 합을 계산합니다.
        long sum = (a + b) * (b - a + 1) / 2; 

        System.out.println(sum);
    }
}

백준 2386번 (Bronze 2)

import java.util.Scanner;

public class problem336 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (true) {
            char one = in.next().charAt(0);

            // 탈출 조건
            if (one == '#') {
                break;
            }

            // trim : 문자열 공백 제거
            String str = in.nextLine().trim();
            int count = 0;

            for (int i = 0; i < str.length(); i++) {
                // 같은 문자인지 검증
                if (Character.toLowerCase(str.charAt(i)) 
                        == Character.toLowerCase(one)) {
                    count++;
                }
            }
            // 출력
            System.out.println(one + " " + count);
        }
    }
}

백준 13163번 (Bronze 2)

import java.util.Scanner;

public class problem337 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int t = in.nextInt();
        in.nextLine(); // 개행 제거

        for (int i = 0; i < t; i++) {
            String[] s = in.nextLine().split(" ");
            String name = "god";
            for (int j = 1; j < s.length; j++) {
                // 문자열 누적
                name += s[j];
            }
            System.out.println(name);
        }
    }
}

백준 13410번 (Bronze 2)

import java.util.Scanner;

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

        int max = 0;
        for (int i = 1; i <= m; i++) {
            int temp = n * i;
            String str = String.valueOf(temp); // 정수를 문자열로 변환
            StringBuilder sb = new StringBuilder();

            // 81 -> 18 뒤집기
            for (int j = str.length() - 1; j >= 0; j--) {
                sb.append(str.charAt(j)); //
            }
            // max값 할당, 문자열을 정수로 변환
            max = Math.max(max, Integer.parseInt(sb.toString()));
        }
        System.out.println(max);
    }
}

0개의 댓글