[알고리즘]/알고리즘

[알고리즘] 백준 2309번 일곱 난쟁이

쿠릉쿠릉 쾅쾅 2022. 2. 13. 17:16
728x90

 

 

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

 

2309번: 일곱 난쟁이

아홉 개의 줄에 걸쳐 난쟁이들의 키가 주어진다. 주어지는 키는 100을 넘지 않는 자연수이며, 아홉 난쟁이의 키는 모두 다르며, 가능한 정답이 여러 가지인 경우에는 아무거나 출력한다.

www.acmicpc.net

 

import java.util.Arrays;
import java.util.Scanner;

public class Prac {

	static int[] shortPeople = new int[9];
	
	static int[] answer = new int[7];
	
   public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
      
      for(int i=0; i<shortPeople.length; i++) {
    	  shortPeople[i] = sc.nextInt();
      }
      combination(0, 0);
   }
   
   static void combination(int n, int start) {
	   if(n==7) {
		   if(Arrays.stream(answer).sum()==100) {
			   Arrays.sort(answer);
			   for(int i=0; i<answer.length; i++) {
				   System.out.println(answer[i]);
			   }
		   }
		   return;
	   }
	   
	   for(int i=start; i<shortPeople.length; i++) {
		   answer[n] = shortPeople[i];
		   combination(n+1, i+1);
	   }
   }
}
728x90