매일 Algorithm

신재원·2023년 4월 24일
0

Algorithm

목록 보기
105/243

백준 15829번 (Bronze 2)

import java.util.Scanner;

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

        int t = in.nextInt();
        String str = in.next();
        int div = 1234567891;
        int multi = 0;
        long result = 1;
        for (int i = 0; i < t; i++) {
            char ch = str.charAt(i);
            // 아스키 값 저장
            multi += ((ch - 'a' + 1) * result) % div;

            // 해시 함수 적용
            result = (result * 31) % div;
        }

        System.out.println(multi % div);
    }
}

백준 15969번 (Bronze 2)

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

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

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

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

        Arrays.sort(arr);

        // 최대값 - 최소값 출력
        System.out.println(arr[arr.length - 1] - arr[0]);
    }
}

백준 16917번 (Bronze 2)

import java.util.Scanner;

public class problem341 {
    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        int a = in.nextInt(); // 양념
        int b = in.nextInt(); // 후라이드
        int c = in.nextInt(); // 반반

        int n = in.nextInt(); // 필요한 양념
        int m = in.nextInt(); // 필요한 후라이드

        int total = 0;

        // 후라이드, 양념이 반반세트보다 비쌀경우
        if (a + b > c * 2) {
            int temp = Math.min(n, m);
            // 최소한의 필요한 양념과 후라이드
            total += c * 2 * temp;

            // 양념이 더 필요한 경우
            if (n > temp) {
                total += (n - temp) * Math.min(a, c * 2);
            }

            // 후라이드가 더 필요한 경우
            if (m > temp) {
                total += (m - temp) * Math.min(b, c * 2);
            }
        }
        // 반반 세트가 아닌 따로 사는게 더 싼 경우
        else {
            total += n * Math.min(a, c * 2);
            total += m * Math.min(b, c * 2);
        }
        System.out.println(total);
    }
}

0개의 댓글