3 条题解

  • 1
    @ 2024-10-22 21:40:06
    #include <iostream>
    using namespace std;
    const int N = 15;
    bool st[N];
    int a[N],n;
    void dfs(int u){
        if(u == n){
            for(int i = 0 ; i < n ; i++){
                cout << a[i]<< ' ';
            }
            cout << '\n';
        }
        for(int i = 1 ; i <= n ; i++){
            if(!st[i]){
                a[u] = i;
                st[i] = 1;
                dfs(u + 1);
                st[i] = 0;
            }
        }
    }
    int main(){
        ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
        cin >> n;
        dfs(0);
        return 0;
    }
    
    • 0
      @ 2025-9-23 15:01:54

      permutations 搞定

      from itertools import permutations
      n = int(input())
      for i in permutations(range(1, n + 1), n):
          print(*i)
      
      • 0
        @ 2024-10-22 19:13:31
        int a[100];
        int cnt;
        int v[100];
        int n;
        void dfs()
        {
        	if(cnt == n)
        	{
        		for(int i = 1 ; i <= cnt ; i++)
        		{
        			printf("%d ",a[i]);
        		}
        		printf("\n");
        		return;
        	}
        	for(int i = 1 ; i <= n ; i++)
        	{
        		if(!v[i])
        		{
        			v[i] = 1;
        			cnt++;
        			a[cnt] = i;
        			dfs();
        			v[i] = 0;
        			cnt--;
        		}
        	}
        }
        int main()
        {
        	scanf("%d", &n);
        	dfs();
        	return 0;
        }
        
        • @ 2024-10-22 20:37:55

          学长好厉害❤️ ❤️ ❤️

      • 1

      信息

      ID
      552
      时间
      1000ms
      内存
      128MiB
      难度
      5
      标签
      (无)
      递交数
      166
      已通过
      67
      上传者