Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

21.04.24 - [BOJ] 15651. N과 M(3) #57

Closed
suhyunsim opened this issue Apr 24, 2021 · 0 comments
Closed

21.04.24 - [BOJ] 15651. N과 M(3) #57

suhyunsim opened this issue Apr 24, 2021 · 0 comments
Assignees
Labels
백트래킹 브루트포스 완전탐색 성공 맞은 문제 실버 BOJ - 실버

Comments

@suhyunsim
Copy link
Owner

suhyunsim commented Apr 24, 2021

문제

핵심 아이디어

  • 1부터 N까지 자연수 중에서 M개를 고른 수열을 모두 구하는 문제
  • N과 M(1)과 비슷 but 중복 선택 가능

어려운 점, 실수

풀이

package main.java.com.poogle.BOJ.Q15651;

import java.util.Scanner;

public class Main {
//    static boolean[] c = new boolean[10]; 중복을 방지하기 위한 배열을 사용하지 않도록 함
    static int[] a = new int[10];
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        System.out.println(go(0, n, m));
    }

    private static StringBuilder go(int index, int n, int m) {
        if (index == m) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < m; i++) {
                sb.append(a[i]);
                if (i != m - 1) sb.append(" ");
            }
            sb.append("\n");
            return sb;
        }
        StringBuilder ans = new StringBuilder();
        for (int i = 1; i <= n; i++) {
//            if (c[i]) continue;
//            c[i] = true;
            a[index] = i;
            ans.append(go(index + 1, n, m));
//            c[i] = false;
        }
        return ans;
    }
}
@suhyunsim suhyunsim self-assigned this Apr 24, 2021
@suhyunsim suhyunsim modified the milestone: 3월 4주 차 Apr 24, 2021
@suhyunsim suhyunsim added the 성공 맞은 문제 label Apr 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
백트래킹 브루트포스 완전탐색 성공 맞은 문제 실버 BOJ - 실버
Projects
None yet
Development

No branches or pull requests

1 participant