๋ฐ์ํ
๋ฌธ์ |
15651๋ฒ: N๊ณผ M (3)
ํ ์ค์ ํ๋์ฉ ๋ฌธ์ ์ ์กฐ๊ฑด์ ๋ง์กฑํ๋ ์์ด์ ์ถ๋ ฅํ๋ค. ์ค๋ณต๋๋ ์์ด์ ์ฌ๋ฌ ๋ฒ ์ถ๋ ฅํ๋ฉด ์๋๋ฉฐ, ๊ฐ ์์ด์ ๊ณต๋ฐฑ์ผ๋ก ๊ตฌ๋ถํด์ ์ถ๋ ฅํด์ผ ํ๋ค. ์์ด์ ์ฌ์ ์์ผ๋ก ์ฆ๊ฐํ๋ ์์๋ก ์ถ๋ ฅํด
www.acmicpc.net
์ฝ๋ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//https://github.com/SiwonLim/Algorithm_PS_2022/blob/master/src/Java/prac_20220316.java | |
import java.io.*; | |
import java.util.*; | |
//BOJ_15651_N๊ณผ M (3) | |
public class Main { | |
static int N,M; | |
static int[] selected; | |
static StringBuilder sb = new StringBuilder(); | |
//input์ ์ธ์์ฃผ์! | |
static void input(){ | |
FastReader fr = new FastReader(); | |
N = fr.nextInt(); | |
M = fr.nextInt(); | |
selected = new int[N+1]; | |
} | |
static void rec_func(int k){ | |
if(k == M+1){//๊ฒฐ๊ณผ๋ฅผ ๋ฆฌํด | |
for(int i = 1 ; i <= M ; i++){ | |
sb.append(selected[i]).append(' '); | |
} | |
sb.append('\n'); | |
}else{//์ฌ๊ท๋ฅผ ํธ์ถ | |
for(int cand = 1 ; cand <= N ; cand++){ | |
selected[k] = cand; | |
//K+1 ~ M๋ฒ ํ์ | |
rec_func(k+1); | |
selected[k] = 0; | |
} | |
} | |
} | |
public static void main(String[] args) { | |
input(); | |
for(int i = 0 ; i < M ; i++){ | |
selected[i]=0; | |
} | |
rec_func(1); | |
System.out.println(sb.toString()); | |
} | |
//์ธ๊ฐ | |
static class FastReader { | |
BufferedReader br; | |
StringTokenizer st; | |
public FastReader() { | |
br = new BufferedReader(new InputStreamReader(System.in)); | |
} | |
String next() { | |
while (st == null || !st.hasMoreElements()) { | |
try { | |
st = new StringTokenizer(br.readLine()); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
return st.nextToken(); | |
} | |
int nextInt() { | |
return Integer.parseInt(next()); | |
} | |
long nextLong() { | |
return Long.parseLong(next()); | |
} | |
double nextDouble() { | |
return Double.parseDouble(next()); | |
} | |
String nextLine() { | |
String str = ""; | |
try { | |
str = br.readLine(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return str; | |
} | |
} | |
} |
๋ฐ์ํ
'๐ค PS(Problem Solving) > ๋ฐฑ์ค(BOJ)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค/java] 15649 - N๊ณผ M (1) (0) | 2021.06.25 |
---|---|
[๋ฐฑ์ค/c++] 11657 - ํ์๋จธ์ (0) | 2019.12.15 |
[๋ฐฑ์ค/c++] 1012๋ฒ - ์ ๊ธฐ๋ ๋ฐฐ์ถ (0) | 2019.12.01 |
[๋ฐฑ์ค/c++] 11729๋ฒ - ํ๋ ธ์ด ํ ์ด๋ ์์ (0) | 2019.11.20 |
[๋ฐฑ์ค/c++] 1107๋ฒ - ๋ฆฌ๋ชจ์ปจ (0) | 2019.11.16 |