BJ2309 일곱 난쟁이

·2022년 4월 17일
0

백준 알고리즘

목록 보기
3/34

https://www.acmicpc.net/problem/2309

조합을 이용해 조건에 만족하는 경우를 찾아 출력.

기본적인 조합문제이다.

package day0209;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class sevenSmalls {
	static BufferedReader br;
	static StringTokenizer st;
	static int[] H;
	static int[] sevenMan = new int[7];
	static boolean found = false;
	
	/// find7 메소드의 매개변수를 활용하여 재귀함수로 구현.
	public static void find7(int count, int sumofH, int start) {
		if(found) return; // 이미 7명의 조합을 찾았으면 return.
		if (sumofH > 100) // 키의 합이 100 이상이면 return.
			return;
		if (count == 7 && sumofH == 100) {
			for (int i = 0; i < 7; i++)
				System.out.printf("%d\n", H[sevenMan[i]]);
			found = true;
			return;
		}
		if(count == 7) return;
		for (int i = start; i < 9; i++) {
			sevenMan[count] = i;
			find7(count + 1, sumofH + H[i], i + 1);
		}
	}

	public static void main(String[] args) throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		H = new int[9];
		for (int i = 0; i < 9; i++) {
			H[i] = Integer.parseInt(br.readLine());
		}
		find7(0,0,0);
	}

}
profile
SSAFY 7기

0개의 댓글