๋ฌธ์ (์ถ์ฒ: https://www.acmicpc.net/problem/21177)
< No Thanks! >
๋ฌธ์ ํ์ด
์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌํ ํ ์๊ฐ ์ฐ์๋์ด ์๋์ง ํ์ธํ๋ค.
my solution (Java)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class _21177_ { // No Thanks!
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int n = Integer.parseInt(bf.readLine());
int arr[] = new int[n];
st = new StringTokenizer(bf.readLine());
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(st.nextToken());
}
Arrays.sort(arr);
int result = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] == arr[i - 1] + 1) {
continue;
}
result += arr[i];
}
System.out.println(result);
}
}
๋ณ์)
n : ๊ฐ์
arr : ์ ๋ณด
result : ๋ต
์ซ์ ๊ฐ์๋ฅผ ์ ๋ ฅ๋ฐ๋๋ค. ์ซ์ ๊ฐ์๋งํผ ๊ฐ์ ์ ๋ ฅ๋ฐ์ ๋ฐฐ์ด์ ์ ์ฅํ๋ค. ๋ฐฐ์ด์ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌํ ํ ์์ฐจ ํ์ํ์ฌ ์ซ์๊ฐ ์ฐ์๋์ง ์์ผ๋ฉด ๊ฐ์ ๋ํ๋ค. ์ต์ข result๋ฅผ ์ถ๋ ฅํ๋ค.
'๐Algorithm > ๐ฅBaekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Baekjoon] 20376_Counting Monuments (0) | 2024.12.10 |
---|---|
[Baekjoon] 11649_Xedni Drawkcab (1) | 2024.12.09 |
[Baekjoon] 14769_Stacking Cups (0) | 2024.12.05 |
[Baekjoon] 6752_Time on task (0) | 2024.12.04 |
[Baekjoon] 21194_Meditation (0) | 2024.12.03 |