๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 9414_ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋Œ€ํšŒ ์ „์šฉ ๋ถ€์ง€

๋ฟŒ์•ผ._. 2024. 10. 23. 10:21
๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/9414)

< ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋Œ€ํšŒ ์ „์šฉ ๋ถ€์ง€ >

 

๋ฌธ์ œ ํ’€์ด 

 

๋น„์šฉ์„ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ์ˆœ์œผ๋กœ ๋•…์„ ๊ตฌ์ž…ํ•œ๋‹ค.

= ๋น„์šฉ์ด ํฐ ๋•…์„ t ๋…„ ํ›„์— ์‚ฌ๋Š” ๊ฒƒ vs ๋น„์šฉ์ด ์ž‘์€ ๋•…์„ t ๋…„ ํ›„์— ์‚ฌ๋Š” ๊ฒƒ ์ค‘์—์„œ ํ›„์ž๊ฐ€ ์ตœ์†Œ ๊ธˆ์•ก์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

 my solution (Java)

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Collections;
public class _9414_ { // ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋Œ€ํšŒ ์ „์šฉ ๋ถ€์ง€
	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		int T = Integer.parseInt(bf.readLine());
		for (int i = 0; i < T; i++) {
			ArrayList<Integer> list = new ArrayList<>();
			int num = -1;
			while ((num = Integer.parseInt(bf.readLine())) != 0) {
				list.add(num);
			}
			Collections.sort(list, Collections.reverseOrder());
			int result = 0;
			boolean flag = false;
			for (int j = 0; j < list.size(); j++) {
				result += 2 * (Math.pow(list.get(j), j + 1));
				if (result > 5000000) {
					flag = true;
					break;
				}
			}
			if (flag) {
				bw.write("Too expensive\n");
			} else {
				bw.write(result + "\n");
			}
		}
		bw.flush();
	}
}
๋ณ€์ˆ˜)
T : ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ๊ฐœ์ˆ˜
list : ๋•…๊ฐ’
result : ์ตœ์†Œ ๊ธˆ์•ก
flag : ๋•… ๊ตฌ์ž… ๊ฐ€๋Šฅ ์—ฌ๋ถ€

 

ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ๊ฐœ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ์ˆ˜๋งŒํผ ๋•…๊ฐ’์„ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด 0์ด ์•„๋‹ ๋•Œ๊นŒ์ง€ ๋•…๊ฐ’์„ ์ž…๋ ฅ๋ฐ›์•„ ArrayList์— ์ €์žฅํ•œ๋‹ค. ๋•…์„ ๊ตฌ์ž…ํ•˜๋Š”๋ฐ ์ตœ์†Œ ๊ธˆ์•ก์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด ArrayList๋ฅผ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค. ArrayList ์ˆœ์œผ๋กœ ๋•…์„ ๊ตฌ์ž…ํ•˜์—ฌ ์ตœ์ข… ๊ธˆ์•ก์ด 5000000๋ณด๋‹ค ํฌ๋ฉด "Too expensive"๋ฅผ ์ž‘์€ ๊ฒฝ์šฐ์—๋Š” ๊ธˆ์•ก์„ ์ถœ๋ ฅํ•œ๋‹ค.



 

'๐ŸŒžAlgorithm > ๐Ÿ”ฅBaekjoon' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Baekjoon] 11968_High Card Wins  (0) 2024.10.25
[Baekjoon] 7596_MP3 Songs  (0) 2024.10.24
[Baekjoon] 5939_Race Results  (0) 2024.10.22
[Baekjoon] 9872_Record Keeping  (0) 2024.10.21
[Baekjoon] 8598_Zajฤ…c  (0) 2024.10.18