4 条题解

题解与来源题共享。已有题解默认通过;新增或编辑后需由来源域管理员审核。待审核、驳回题解仅作者和审核管理员可见。来源域:system

  • 1
    @ 2026-5-21 17:34:28

    提供一个py的状态压缩的解法

    n = int(input())
    for i in range(1 << n):
        print(*(j+1 for j in range(n) if i >> (n - j - 1) & 1))
    
    • 0
      @ 2026-11-2 21:12:59

      想按字典序输出调换选和不选顺序就行

      #include<bits/stdc++.h>
      using namespace std;
      using ll=long long;
      int n;
      vector<int>path;
      void dfs(int x){
      	if(x>n){
      		for(auto i:path)cout<<i<<" ";
      		cout<<endl;
      		return ;
      	}
      		//不选
      		dfs(x+1);
      		//选
      		path.push_back(x);
      		dfs(x+1);
      		path.pop_back();
      	
      }
      
      int main(){
      	cin>>n;
          dfs(1);
      	
      }      
      
      
      • 0
        @ 2026-6-2 21:56:44
        #include <bits/stdc++.h>
        using namespace std;
        #define ll long long
        const int N = 2e5+10;
        ll n;
        vector<int> p;
        
        void dfs(int num){
        	if(num == n+1){
        		for(int i = 0;i < p.size();i++){
        			if(i == 0) cout<<p[i];
        			else cout<<" "<<p[i];
        		}
        		cout<<"\n";
        		return;
        	}
          
          // 先不选
        	dfs(num+1);
          
        	p.push_back(num);
        	dfs(num+1);
        	p.pop_back(); // 回溯
        } 
        
        int main(){
        	ios::sync_with_stdio(false);
        	cin.tie(0);
        	cin>>n;
        	dfs(1);
        	return 0;
        }
        
        • 0
          @ 2025-5-9 18:23:03

          JAVA ac代码:

          import java.util.*;
          import java.util.StringTokenizer;
          import java.io.*;
          
          public class Main {
              static int n;
              static boolean flag[];
              public static void main(String[] args) {
                  n=in.nextInt();
                  flag=new boolean[n+1];
                  dfs(1);
                  out.close();
              }
          
              private static void dfs(int i) {
                  //终止条件:i>n的时候,已经处理完了1-n
                  if(i>n) {
                      for(int j=1;j<=n;j++) {
                          //选了这个数
                          if(flag[j]) {
                              out.print(j+" ");
                          }
                      }
                      out.println();
                      return;
                  }
                  //题目会先输出选0个的选项,所以要把不选的dfs放前面
                  dfs(i+1);
                  flag[i]=true;
                  dfs(i+1);
                  flag[i]=false;//回溯
              }
          
          
              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
          498
          时间
          1000ms
          内存
          256MiB
          难度
          普及−
          标签
          递交数
          0
          已通过
          0
          上传者