• Home
  • About
    • Ryureka Moment photo

      Ryureka

      Sin Prisa, Sin Pausa

    • About Me
    • Facebook
    • Github
    • Youtube
  • Projects
  • Posts
    • Posts
    • ProblemSolvings
    • Tags
    • Blog
    • Examples
  • ProblemSolving
    • ProblemSolving
    • BOJ
    • Programmers
    • SWEA
    • LeetCode
  • FrontEnd
    • FrontEnd
    • HTML
  • BackEnd
    • BackEnd
    • Server
      • Server
      • Spring
      • NodeJS
    • DataBase
      • DataBase
      • MySQL
      • MongoDB
  • Programming
    • Programming
    • Java
    • JS
    • Python
    • CleanCode
  • ComputerScience
    • DataStructure
    • Algorithm

[BOJ] 15655. N과 M(6)

15 Apr 2026

Reading time ~1 minute

  • 풀이

풀이

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

public class Main {
	static int N,M;
	static int arr[];
	static boolean checked[];
	static int select[];
	static Scanner sc = new Scanner(System.in);
	public static void main(String[] args) {
		N = sc.nextInt();
		M = sc.nextInt();		
		arr = new int[N];
		select = new int[M];
		checked = new boolean[N];
		for (int i = 0; i < N; i++) {
			arr[i] = sc.nextInt();
		}
		Arrays.sort(arr);
		go(0,0);
	}
	
	public static void go(int d,int start) {
		if(d == M) {
			for (int i = 0; i < select.length; i++) {
				System.out.print(select[i]+" ");
			}
			System.out.println();
			return;
		}
		
		for (int i = start; i < N; i++) {
			if(!checked[i]) {
				checked[i]=true;
				select[d]=arr[i];
				go(d+1,i);
				checked[i]=false;
				select[d]=0;
			}
		}
	}
}


백트래킹조합 Share