3 条题解

  • 3
    @ 2023-5-2 19:16:03
    #include <iostream>
    #include <string.h>
    #include <cstdio>
    #include <algorithm>
    using namespace std;
    #define LL long long
    int n , a[15];
    bool l[15];
    
    inline void print()
    {
    for (int i = 0 ; i < n ; ++i)
    {
    putchar(a[i] + 48);
    putchar(32);
    }
    putchar(10);
    }
    
    void f(int m)
    {
    if (m == n)
    {
    print();
    return;
    }
    for (int i = 1 ; i <= n ; ++i)
    {
    if (l[i])
    continue;
    l[i] = 1;
    a[m] = i;
    f(m + 1);
    l[i] = 0;
    }
    return;
    }
    
    int main()
    {
    memset(l , 0 , sizeof(l));
    cin >> n;
    f(0);
    return 0;
    }
    
    
    • 1
      @ 2023-10-29 17:31:53

      #include <bits/stdc++.h> using namespace std;

      int n; int res[1234567]; bool a[1234567];

      void print () { for (int i = 1; i <= n; i++) { cout << res[i] << " "; } cout << endl; }

      void dfs (int cur) { if (cur > n) { print (); return ; } for (int i = 1; i <= n; i++) { if (!a[i]) { a[i] = true; res[cur] = i; dfs (cur + 1); a[i] = false; } } }

      int main() { cin >> n; dfs (1); return 0; }

      纯分享

      • 0
        @ 2023-4-1 19:58:06
        #include <iostream>
        #include <string.h>
        #include <cstdio>
        #include <algorithm>
        using namespace std;
        #define LL long long
        int n , a[15];
        bool l[15];
         
        inline void print()
        {
        	for (int i = 0 ; i < n ; ++i)
        	{
        		putchar(a[i] + 48);
        		putchar(32);
        	} 
        	putchar(10);
        }
         
        void f(int m)
        {
        	if (m == n)
        	{
        		print();
        		return;
        	}
        	for (int i = 1 ; i <= n ; ++i)
        	{
        		if (l[i])
        			continue;
        		l[i] = 1;
        		a[m] = i;
        		f(m + 1);
        		l[i] = 0;
        	}
        	return;
        }
         
        int main()
        {
        	memset(l , 0 , sizeof(l));
        	cin >> n;
        	f(0);
        	return 0;
        }
        
        • 1

        信息

        ID
        7
        时间
        1000ms
        内存
        128MiB
        难度
        2
        标签
        递交数
        330
        已通过
        209
        上传者