๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 25496_์žฅ์‹ ๊ตฌ ๋ช…์žฅ ์ž„์Šค

๋ฟŒ์•ผ._. 2023. 10. 27. 10:14

Silver V

๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/25496)

< ์žฅ์‹ ๊ตฌ ๋ช…์žฅ ์ž„์Šค >

 

๋ฌธ์ œ ํ’€์ด 

 

ํ”ผ๋กœ๋„๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ํ›„ ์ดํ”ผ๋กœ๋„๊ฐ€ 200 ๋ฏธ๋งŒ์ผ ๋•Œ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ์žฅ์‹ ๊ตฌ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•œ๋‹ค.

 

 

 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 _25496_ { // ์žฅ์‹ ๊ตฌ ๋ช…์žฅ ์ž„์Šค

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

		int p = Integer.parseInt(st.nextToken());
		int n = Integer.parseInt(st.nextToken());

		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 = 0;
		for (int i = 0; i < n; i++) {
			if (p >= 200)
				break;
			result += 1;
			p += arr[i];
		}
		System.out.println(result);
	}

}

 

Main

๋ณ€์ˆ˜)
p : ํ˜„์žฌ ์Œ“์ธ ํ”ผ๋กœ๋„
n : ์žฅ์‹ ๊ตฌ ์ˆ˜
arr : ์žฅ์‹ ๊ตฌ๋ฅผ ๋งŒ๋“ค ๋•Œ ์Œ“์ด๋Š” ํ”ผ๋กœ๋„
result : ์ œ์ž‘ํ•  ์ˆ˜ ์žˆ๋Š” ์žฅ์‹ ๊ตฌ์˜ ์ตœ๋Œ€ ์ˆ˜

 

- ํ˜„์žฌ ์Œ“์ธ ํ”ผ๋กœ๋„(p), ์žฅ์‹ ๊ตฌ ์ˆ˜(n) ์ž…๋ ฅ

- ํ”ผ๋กœ๋„ ์ž…๋ ฅ๋ฐ›์•„ ์ €์žฅ(arr)

- arr ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ

- arr์„ ์ˆœํ™˜ํ•˜๋ฉฐ ์ดํ”ผ๋กœ๋„๊ฐ€ 200 ๋ฏธ๋งŒ์ผ ๋•Œ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ์žฅ์‹ ๊ตฌ ์ˆ˜ ๊ตฌํ•˜๊ธฐ

- ์ œ์ž‘ํ•  ์ˆ˜ ์žˆ๋Š” ์žฅ์‹ ๊ตฌ์˜ ์ตœ๋Œ€ ์ˆ˜(result) ์ถœ๋ ฅ