๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 9575_ํ–‰์šด์˜ ์ˆ˜

๋ฟŒ์•ผ._. 2023. 10. 24. 13:28

Silver IV

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

< ํ–‰์šด์˜ ์ˆ˜ >

 

๋ฌธ์ œ ํ’€์ด 

 

A, B, C ์ˆ˜์—ด์—์„œ ํ•˜๋‚˜์”ฉ ๊ณจ๋ผ ํ–‰์šด์˜ ์ˆ˜๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด๋ฏ€๋กœ 3์ค‘ for๋ฌธ์„ ์‚ฌ์šฉํ•ด์„œ ๊ตฌํ•œ๋‹ค.

 

 

 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.HashSet;
import java.util.Set;
import java.util.StringTokenizer;

public class _9575_ { // ํ–‰์šด์˜ ์ˆ˜

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

		int t = Integer.parseInt(bf.readLine());

		for (int i = 0; i < t; i++) {
			int n = Integer.parseInt(bf.readLine());
			st = new StringTokenizer(bf.readLine());
			int a[] = new int[n];
			for (int j = 0; j < n; j++) {
				a[j] = Integer.parseInt(st.nextToken());
			}

			int m = Integer.parseInt(bf.readLine());
			st = new StringTokenizer(bf.readLine());
			int b[] = new int[m];
			for (int j = 0; j < m; j++) {
				b[j] = Integer.parseInt(st.nextToken());
			}

			int k = Integer.parseInt(bf.readLine());
			st = new StringTokenizer(bf.readLine());
			int c[] = new int[k];
			for (int j = 0; j < k; j++) {
				c[j] = Integer.parseInt(st.nextToken());
			}

			Set<Integer> set = new HashSet<>();
			int result = 0;
			for (int j = 0; j < n; j++) {
				for (int l = 0; l < m; l++) {
					for (int o = 0; o < k; o++) {
						result = a[j] + b[l] + c[o];

						String temp = Integer.toString(result);
						if (!(temp.contains("0") || temp.contains("1") || temp.contains("2") || temp.contains("3")
								|| temp.contains("4") || temp.contains("6") || temp.contains("7")
								|| temp.contains("9"))) {
							set.add(result);
						}
					}
				}
			}
			bw.write(set.size() + "\n");
		}
		bw.flush();
	}
}

 

Main

๋ณ€์ˆ˜)
t : ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ์ˆ˜
n : A์˜ ํฌ๊ธฐ
a : A์˜ ์›์†Œ
m : B์˜ ํฌ๊ธฐ
b : B์˜ ์›์†Œ
k : C์˜ ํฌ๊ธฐ
c : C์˜ ์›์†Œ
set : ํ–‰์šด์˜ ์ˆ˜ ์ €์žฅ
result : ํ–‰์šด์˜ ์ˆ˜

 

- ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ์ˆ˜(t) ์ž…๋ ฅ

- ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ์ˆ˜(t) ๋งŒํผ ์ˆ˜์—ด ์ž…๋ ฅ

: A์˜ ํฌ๊ธฐ(n), ์ˆ˜์—ด A์˜ ์›์†Œ(a) ์ž…๋ ฅ๋ฐ›์•„ ์ €์žฅ

: B์˜ ํฌ๊ธฐ(m), ์ˆ˜์—ด B์˜ ํฌ๊ธฐ(b) ์ž…๋ ฅ๋ฐ›์•„ ์ €์žฅ

: C์˜ ํฌ๊ธฐ(k), ์ˆ˜์—ด C์˜ ํฌ๊ธฐ(c) ์ž…๋ ฅ๋ฐ›์•„ ์ €์žฅ

: A, B, C์—์„œ ํ•˜๋‚˜์”ฉ ๊ณจ๋ผ ํ•ฉ์„ ๊ตฌํ•จ(result)

: result๊ฐ€ 5์™€ 8๋กœ๋งŒ ์ด๋ค„์ ธ ์žˆ๋‹ค๋ฉด set์— ์ถ”๊ฐ€

- ํ–‰์šด์˜ ์ˆ˜ ๊ฐœ์ˆ˜์ธ set์˜ ํฌ๊ธฐ ์ถœ๋ ฅ



 

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

[Baekjoon] 5648_์—ญ์›์†Œ ์ •๋ ฌ  (1) 2023.10.26
[Baekjoon] 11637_์ธ๊ธฐ ํˆฌํ‘œ  (0) 2023.10.25
[Baekjoon] 1058_์นœ๊ตฌ  (1) 2023.10.23
[Baekjoon] 1956_์šด๋™  (0) 2023.10.20
[Baekjoon] 1015_์ˆ˜์—ด ์ •๋ ฌ  (0) 2023.10.19