매일 Algorithm

신재원·2023년 4월 10일
0

Algorithm

목록 보기
92/243

백준 5347번 (Silver 5)

import java.util.Scanner;

public class problem294 {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        int t = in.nextInt();

        for (int i = 0; i < t; i++) {
            int n = in.nextInt();
            int m = in.nextInt();
            long result = lcm(n, m);
            System.out.println(result);
        }
    }

    // 최대 공약수
    static long gcd(int a, int b) {
        if (b == 0) return a;
        return gcd(b, a % b);
    }

    // 최소 공배수
    static long lcm(int a, int b) {
        return (long) a * b / gcd(a, b);
    }
}

백준 5555번 (Silver 5)

import java.util.Scanner;

public class problem295 {

    public static void main(String[] args) {

        // 간단한 문자열 문제다.
        Scanner in = new Scanner(System.in);
        String str = in.next();
        int t = in.nextInt();
        int result = 0;
        for (int i = 0; i < t; i++) {
            String s = in.next();
            s += s;
            // .contains : 문자열의 포함되어있는지 확인
            if (s.contains(str)) {
                result++;
            }
        }
        System.out.print(result);
    }
}

백준 5800번 (Silver 5)

import java.util.Arrays;
import java.util.Scanner;

public class problem296 {

    public static void main(String[] args) {


        Scanner in = new Scanner(System.in);
        int t = in.nextInt(); // 반의 수


        for (int i = 0; i < t; i++) {
            int n = in.nextInt(); // 학생수

            int[] arr = new int[n];
            int gap = 0;

            for (int j = 0; j < n; j++) {
                arr[j] = in.nextInt();
            }

            Arrays.sort(arr);

            // 30 25 76 23 78
            // 23 25 30 76 78
            for (int k = 0; k < n - 1; k++) {
             	// 값의 차이중 최대값
                gap = Math.max(gap, arr[k + 1] - arr[k]);
            }

            System.out.println("Class " + (i + 1));
            System.out.println("Max " + arr[arr.length - 1] +
            ", " + "Min " + arr[0] + ", " + "Largest gap " + gap);

        }
    }
}

백준 10867번 (Silver 5)

import java.util.*;

public class problem297 {

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

        // 중복제거를 위해 set 컬렉션
        Set<Integer> set = new HashSet<>();
        for (int i = 0; i < n; i++) {
            set.add(in.nextInt());
        }

        List<Integer> list = new ArrayList<>(set);
        Collections.sort(list);

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


    }
}

0개의 댓글