1 条题解

  • -1
    @ 2025-5-9 18:14:55

    JAVA ac代码:

    import java.util.*;
    import java.util.StringTokenizer;
    import java.io.*;
    
    public class Main {
        static int n,m;
        public static void main(String[] args) {
            n=in.nextInt();m=in.nextInt();
            int[] arr=new int[m+10];
            dfs(arr,1,0);
            out.close();
        }
        
    
    
    
        private static void dfs(int[] arr,int i, int count) {
            //i越界或者剩余的i不足够填满m个数字
            if(i>n+1||count+(n-i)<m-2)return;
        
            //如果count==m,数量够了就输出
            if(count==m) {
                for(int j=0;j<m;j++) out.print(arr[j]+" ");
                out.println();
                return;
            }
            arr[count]=i;
            dfs(arr,i+1,count+1);
            arr[count]=0;
            dfs(arr,i+1,count);
            
        }
    
    
    
    
        static FastReader in=new FastReader();
        static PrintWriter out=new PrintWriter(System.out);
        static class FastReader{
            static BufferedReader br;
            static StringTokenizer st;
            FastReader(){
                br=new BufferedReader(new InputStreamReader(System.in));
            }
            String next() {
                String str="";
                while(st==null||!st.hasMoreElements()) {
                    try {
                        str=br.readLine();
                    }catch(IOException e) {
                        throw new RuntimeException(e);
                    }
                    st=new StringTokenizer(str);
                }
                return st.nextToken();
            }
            int nextInt() {
                return Integer.parseInt(next());
            }
            double nextDouble() {
                return Double.parseDouble(next());
            }
            long nextLong() {
                return Long.parseLong(next());
            }
        }
        
    }
    
    • 1

    信息

    ID
    34
    时间
    1000ms
    内存
    256MiB
    难度
    2
    标签
    递交数
    41
    已通过
    25
    上传者