๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 21177_No Thanks!

๋ฟŒ์•ผ._. 2024. 12. 6. 16:55
๋ฌธ์ œ(์ถœ์ฒ˜: 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