[๋ฐฑ์ค] ๐ฅ 1059 ์ข์ ๊ตฌ๊ฐ
๐ ๋์ด๋
๐ฅ Silver 4
๐ ๋ฌธ์
https://www.acmicpc.net/problem/1059
๐ ํ์ด
๋ฌธ์ ๋ฅผ ์ฒ์ ๋ณด์๋ง์ ์ผ๋จ 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
์ผ๋ก ๋ณ๊ฒฝํ๋ฉด ๋๋ค.
๊ฐ์ธ ๊ณต๋ถ ๊ธฐ๋ก์ฉ ๋ธ๋ก๊ทธ์
๋๋ค.
ํ๋ฆฌ๊ฑฐ๋ ์ค๋ฅ๊ฐ ์์ ๊ฒฝ์ฐ ์ ๋ณดํด์ฃผ์๋ฉด ๊ฐ์ฌํ๊ฒ ์ต๋๋ค.๐