1 minute read

๐Ÿ“Œ ๋‚œ์ด๋„

๐Ÿฅˆ Silver 4


๐Ÿ“Œ ๋ฌธ์ œ

https://www.acmicpc.net/problem/1059

image



๐Ÿ“Œ ํ’€์ด

๋ฌธ์ œ๋ฅผ ์ฒ˜์Œ ๋ณด์ž๋งˆ์ž ์ผ๋‹จ n์ด ์ง‘ํ•ฉ S์˜ ์–ด๋Š ์ˆซ์ž ์‚ฌ์ด์— ์žˆ๋Š”์ง€ ๋ฝ‘์•„๋‚ด๊ณ  ์ƒ๊ฐํ•˜๊ธฐ๋กœ ํ•˜์˜€๋‹ค.
n๋ณด๋‹ค ์ž‘์€ ์ˆซ์ž๋ฅผ ๋ฝ‘์•„ min์— ๋„ฃ๊ณ , S์˜ ์ˆซ์ž๊ฐ€ ๋” ์ปค์ง€๋ฉด ๋‹ค์Œ ์ˆซ์ž๋ฅผ max์— ๋„ฃ์—ˆ๋‹ค.
์ด๋•Œ ๋งŒ์•ฝ max == n์ด๋ผ๋ฉด 0์„ ์ถœ๋ ฅํ•˜์˜€๋‹ค.
์ดํ›„ ๊ตฌํ•ด์ง„ ๊ตฌ๊ฐ„์ด ๋‹ค์Œ๊ณผ ๊ฐ™์ด min, n, max๊ฐ€ ๋‚˜์˜ค๋Š”๋ฐ ์ดํ›„ ์ฝ”๋“œ์ฒ˜๋Ÿผ ์ฒ˜๋ฆฌํ•˜์˜€๋‹ค.


๐Ÿ“Œ Code

package Math;

import java.io.*;
import java.util.Arrays;
import java.util.StringTokenizer;

public class N1059 {
    public static int[] sets;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());

        sets = new int[Integer.parseInt(st.nextToken())];
        st = new StringTokenizer(br.readLine());
        int index = 0;
        while (st.hasMoreTokens()) {
            sets[index++] = Integer.parseInt(st.nextToken());
        }
        Arrays.sort(sets);

        st = new StringTokenizer(br.readLine());
        int num = Integer.parseInt(st.nextToken());

        Section(num);
    }

    public static void Section(int num) {
        int min = 0;
        int max = 0;

        int index = 0;
        while (num > sets[index]) {
            min = sets[index++];
        }
        max = sets[index];
        if (max == num) {
            System.out.println(0);
        }
        else{
            System.out.println(Counts(num, min) * Counts(max, num) - 1);
        }
    }

    public static int Counts(int max, int min){
        return max - min;
    }
}

package (์ด๋ฆ„); ๋ฅผ ๋•Œ๊ณ , class ์ด๋ฆ„์„ Main์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋ฉด ๋œ๋‹ค.



๊ฐœ์ธ ๊ณต๋ถ€ ๊ธฐ๋ก์šฉ ๋ธ”๋กœ๊ทธ์ž…๋‹ˆ๋‹ค.
ํ‹€๋ฆฌ๊ฑฐ๋‚˜ ์˜ค๋ฅ˜๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ ์ œ๋ณดํ•ด์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.๐Ÿ˜