5 条题解

  • 2
    @ 2023-8-27 10:59:28
    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
        int n, num[10];
        cin >> n;
        for (int i = 1; i <= n; i++)
            num[i] = i;
        do
        {
            for (int i = 1; i <= n; i++)
                cout << num[i] << " ";
            cout << endl;
        }while(next_permutation(num + 1, num + n + 1));
        return 0;
    }
    

    e......

    • 2
      @ 2022-10-9 22:04:08
      #include<bits/stdc++.h>
      using namespace std;
      int n,a[10];
      bool vis[10];
      void dfs(int cnt){
          if(cnt==n+1){
              for(int i=1;i<=n;i++)cout<<a[i]<<" ";
              cout<<endl;
              return;
          }
          for(int i=1;i<=n;i++)
              if(!vis[i]){
                  vis[i]=1;
                  a[cnt]=i;
                  dfs(cnt+1);
                  vis[i]=0;
              }
      }
      int main(){
          cin>>n;
          dfs(1);
          return 0;
      }
      
      • 1
        @ 2024-6-2 12:40:22

        提示:本题解作者只是为了练习语法,思路讲的可能很多

        #include <bits/stdc++.h>
        using std::cin;
        using std::cout;
        using std::endl;
        using std::next_permutation;
        using ll = long long;
        int n;
        namespace sub1   // 如果小于5,我们可以用dfs解
        {
            int a[10];
            bool vis[10];
            void dfs(int u)
            {
                if (u > n)
                {
                    for (int i = 1; i <= n; i++)
                        cout << a[i] << " ";
                    cout << endl;
                    return ;
                }
                for (int i = 1; i <= n; i++)
                {
                    if (!vis[i])
                    {
                        vis[i] = 1;
                        a[u] = i;
                        dfs(u + 1);
                        vis[i] = 0;
                    }
                }
            }
            void main()
            {
                
                dfs(1);
            }
        }
        namespace sub2     // 剩下的情况都用next_permutation解
        {
            int a[15];
            void main()
            {
                for (int i = 1; i <= n; i++)
                    a[i] = i;
                do
                {
                    for (int i = 1; i <= n; i++)
                        cout << a[i] << " ";
                    cout << endl;
                }while (next_permutation(a + 1, a + n + 1));
            }
        }
        int main()
        {
            cin >> n;
            if (n <= 5)
                sub1::main();
            else
                sub2::main();
            return 0;
        }
        
        • 0
          @ 2023-10-1 10:09:14
          #include <iostream>
          #include <algorithm>
          using namespace std;
          int main(){
              int n;
              cin>>n;
              int a[n];
              for(int i=0;i<n;i++)a[i]=i+1;
              do {
                  for(int i=0;i<n;i++)cout<<a[i]<<' ';
                  cout<<endl;
              }while (next_permutation(a,a+n));
              return 0;
          }
          
          • 0
            @ 2023-2-11 14:53:40

            这道题难度还阔以 你们知道吗在#include <algorithm>里有一个函数,全排列函数:

            next_permutation!

            要循环n的阶乘次! 上代码:

            using namespace std;
            int a[10];
            int main()
            {
            	int n,i,j=1,k;
            	cin>>n;
            	for(i=1;i<=n;i++)
            	{
            	    a[i]=n-i+1;j*=i;
            	    
            	}//题目好像没说要从小到大输出
                 //但保险起见还是初始赋值为最大序列
                 //即a[1~n]=n~1;顺便计算n!
            	for(i=1;i<=j;i++)
            	{
            	    next_permutation(a+1,a+n+1);
            	    for(k=1;k<=n;k++)
            	        cout<<"    "<<a[k];//排一次输出一次
            	    cout<<endl;
            	 }
            	 return 0;
            }
            

            其实我知道这个是在##[洛谷]编程的题解里知道的 网址:https://www.luogu.com.cn/problem/solution/P1706

            • 1

            信息

            ID
            305
            时间
            1000ms
            内存
            16MiB
            难度
            2
            标签
            递交数
            57
            已通过
            36
            上传者