๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 7490_0 ๋งŒ๋“ค๊ธฐ

๋ฟŒ์•ผ._. 2024. 4. 30. 10:59

Gold V

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

< 0 ๋งŒ๋“ค๊ธฐ >

 

๋ฌธ์ œ ํ’€์ด 

 

'+', '-', ' '์„ ๋„ฃ์„ ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ํƒ์ƒ‰ํ•œ๋‹ค.

 

 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;

public class _7490_ { // 0 ๋งŒ๋“ค๊ธฐ
	static ArrayList<String> answer;

	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++) {
			int n = Integer.parseInt(bf.readLine());

			String result = "1";
			answer = new ArrayList<>();
			search(result, n, 2);

			for(String str:answer) {
				bw.write(str+"\n");
			}
			bw.write("\n");
		}
		bw.flush();
	}

	private static void search(String result, int n, int idx) {
		if (idx == n + 1) {
			String temp = result.replace(" ", "");
			int x = 1;
			int a = temp.charAt(0) - '0';
			while (x < temp.length() && Character.isDigit(temp.charAt(x))) {
				a *= 10;
				a += temp.charAt(x++)-'0';
			}
			while (x < temp.length()) {
				int y = x;
				y += 1;
				int b = temp.charAt(y) - '0';
				while (y + 1 < temp.length() && Character.isDigit(temp.charAt(y + 1))) {
					b *= 10;
					b += temp.charAt(++y) - '0';
				}
				if (temp.charAt(x) == '+') {
					a += b;
				} else {
					a -= b;
				}
				x = y + 1;
			}
			if (a == 0) {
				answer.add(result);
			}
			return;
		}

		search(result + " " + idx, n, idx + 1);
		search(result + "+" + idx, n, idx + 1);
		search(result + "-" + idx, n, idx + 1);

	}
}
๋ณ€์ˆ˜)
t : ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ๊ฐœ์ˆ˜
n : ์ž์—ฐ์ˆ˜ N
result : ์ˆ˜์‹
answer : 0์ด ๋˜๋Š” ์ˆ˜์‹

 

ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ์ˆ˜ t๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ์ˆ˜๋งŒํผ ์ž์—ฐ์ˆ˜ N์„ ์ž…๋ ฅ๋ฐ›์•„ search ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค. ์ตœ์ข… answer ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.

 

search(์ˆ˜์‹, ์ž์—ฐ์ˆ˜ N, ํ˜„์žฌ ๊ฐ’)

ํ˜„์žฌ ๊ฐ’์ด n+1์ด๋ผ๋ฉด ์ˆ˜์‹์„ ๊ณ„์‚ฐํ•œ๋‹ค. ๊ณ„์‚ฐ์„ ์œ„ํ•ด ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ•œ๋‹ค. ๋จผ์ € ๊ฐ’ a๋ฅผ ์ˆ˜์‹์˜ 0๋ฒˆ์งธ ๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค. ์ˆซ์ž๋ฅผ ์ด์–ด ๋ถ™์ธ ๊ฒƒ์„ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์Œ ๊ฐ’์ด ์ˆซ์ž์ธ์ง€ ํ™•์ธํ•˜๋ฉฐ ์ˆซ์ž๋ผ๋ฉด ์ด์–ด ๋ถ™์ด๊ณ  ์ˆซ์ž๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด ๋‹ค์Œ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•œ๋‹ค.

 

๊ฐ’ b๋ฅผ ์ˆ˜์‹์˜ y๋ฒˆ์งธ ๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค. ์ˆซ์ž๋ฅผ ์ด์–ด ๋ถ™์ธ ๊ฒƒ์„ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์Œ ๊ฐ’์ด ์ˆซ์ž์ธ์ง€ ํ™•์ธํ•˜๋ฉฐ ์ˆซ์ž๋ผ๋ฉด ์ด์–ด ๋ถ™์ธ๋‹ค. b๊ฐ’์„ ๊ตฌํ•œ ๋’ค x๋ฒˆ์งธ ๊ฐ’์ด +๋ผ๋ฉด a์™€ b๋ฅผ ๋”ํ•˜๊ณ , -๋ผ๋ฉด a์™€ b๋ฅผ ๋บ€๋‹ค.

 

๋งŒ์•ฝ ๊ฒฐ๊ณผ๊ฐ€ 0์ด๋ผ๋ฉด answer์— ์ˆ˜์‹์„ ์ถ”๊ฐ€ํ•œ ํ›„ ์ข…๋ฃŒํ•œ๋‹ค.

 

๊ณต๋ฐฑ์„ ์‚ฝ์ž…ํ•˜์—ฌ search ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

+๋ฅผ ์‚ฝ์ž…ํ•˜์—ฌ search ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

-๋ฅผ ์‚ฝ์ž…ํ•˜์—ฌ search ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.