๐ ๋ฌธ์ ๋งํฌ
๐ ํ์ด ๊ณผ์
- ์ฌ๊ท ์ ํ์ ๋ฌธ์ ์
๋๋ค.
- ๋น๋ด๋ฆผ์ฐจ์์ผ๋ก ์ถ๋ ฅ์ ํ๋ ๊ฒ์ด ์กฐ๊ฑด๋ฌธ์ ์ค์ ํ๋๋ฐ ์ค์ํ ๋ถ๋ถ์
๋๋ค.
- ๋งค๊ฐ๋ณ์๊ฐ 0์ผ๋๋ ์ผ๋จ ๋ฃ์ด์ผ ํ๊ธฐ์, answer ๋ฐฐ์ด ๋ณ์์ ๊ฐ์ ํ ๋นํฉ๋๋ค.
- ๊ทธ ์ธ์ ๊ฐ์ ํ ๋นํ ๋๋ ๋ชจ๋ ๊ฒฝ์ฐ์ ์์์ ๋น๋ด๋ฆผ์ฐจ์์ธ์ง๋ง ํ์ธํ์ฌ ๊ฐ์ ํ ๋นํ๋์์ผ๋ก ๊ตฌํํ์ต๋๋ค.
๐ป ์ฝ๋
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Main {
static int n,m;
static int[] arr,answer;
static boolean[] visited;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine()," ");
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
st = new StringTokenizer(br.readLine()," ");
arr = new int[n];
answer = new int[m];
visited = new boolean[n];
for(int i = 0; i< n; i++){
arr[i] = Integer.parseInt(st.nextToken());
}
Arrays.sort(arr);
solve(0);
}
public static void solve(int idx){
if(idx == m){
for(int i = 0; i< m; i++){
System.out.print(answer[i] + " ");
}
System.out.println();
return;
}
for(int i = 0; i< n; i++){
if(idx == 0){
answer[idx] = arr[i];
solve(idx + 1);
}
else if(answer[idx - 1] <= arr[i] ){
answer[idx] = arr[i];
solve(idx + 1);
}
}
}
}
'Algorithm > ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ฐฑ์ค 11660๋ฒ : ๊ตฌ๊ฐ ํฉ ๊ตฌํ๊ธฐ 5(Java) (0) | 2022.01.17 |
---|---|
๋ฐฑ์ค 1149๋ฒ : RGB๊ฑฐ๋ฆฌ (Java) (0) | 2022.01.17 |
๋ฐฑ์ค 15654๋ฒ : N๊ณผ M (5) (Java) (0) | 2022.01.17 |
๋ฐฑ์ค 15652๋ฒ : N๊ณผ M (4) (Java) (0) | 2022.01.17 |
๋ฐฑ์ค 11659๋ฒ : ๊ตฌ๊ฐ ํฉ ๊ตฌํ๊ธฐ 4(Java) (0) | 2022.01.17 |