洛谷 B3621 枚举元组 Java

import java.util.*;
import java.io.*;

public class Main{

    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
    static StringBuilder result = new StringBuilder();
    static int n,k;
    
    public static void main(String[] args) throws IOException {
        String[] S = br.readLine().split(" ");
        n = Integer.parseInt(S[0]);
        k = Integer.parseInt(S[1]);
        DFS(0);
        out.flush();
        out.close();
        br.close();
    }
    
    private static void DFS(int layer) {
    	if(layer == n) {
    		System.out.println(result);
    		return ;
    	}
    	for(int i = 1 ; i <= k ; i++) {
    		result.append(i + " ");
    		layer++;
    		DFS(layer);
    		layer--;
    		result.deleteCharAt(result.length()-1);
    		result.deleteCharAt(result.length()-1);
    	}
    }
}

你可能感兴趣的:(洛谷刷题之旅,java,算法,数据结构)