zoukankan      html  css  js  c++  java
  • 打败大魔王之最小排列数问题(全排列)

    Now our hero finds the door to the BEelzebub feng5166. He opens the door and finds feng5166 is about to kill our pretty Princess. But now the BEelzebub has to beat our hero first. feng5166 says, "I have three question for you, if you can work them out, I will release the Princess, or you will be my dinner, too." Ignatius says confidently, "OK, at last, I will save the Princess." 

    "Now I will show you the first problem." feng5166 says, "Given a sequence of number 1 to N, we define that 1,2,3...N-1,N is the smallest sequence among all the sequence which can be composed with number 1 to N(each number can be and should be use only once in this problem). So it's easy to see the second smallest sequence is 1,2,3...N,N-1. Now I will give you two numbers, N and M. You should tell me the Mth smallest sequence which is composed with number 1 to N. It's easy, isn't is? Hahahahaha......" 
    Can you help Ignatius to solve this problem? 

    输入
    6 4 11 8
    输出
    1 2 3 5 6 4 1 2 3 4 5 6 7 9 8 11 10

    就是将其全排列后从小到大排列的第M个数,用next_permutation很轻松的解决了,这个stl能将要排列的数从小到大排序,找到对应的输出极好了,简单提一下这个函数的用法
    在STL中,除了next_permutation外,还有一个函数prev_permutation,两者都是用来计算排列组合的函数。前者是求出下一个排列组合,而后者是求出上一个排列组合。所谓“下一个”和“上一个”,书中举了一个简单的例子:
    对序列 {a, b, c},每一个元素都比后面的小,按照字典序列,固定a之后,a比bc都小,c比b大,它的下一个序列即为{a, c, b},而{a, c, b}的上一个序列即为{a, b, c},
    同理可以推出所有的六个序列为:{a, b, c}、{a, c, b}、{b, a, c}、{b, c, a}、{c, a, b}、{c, b, a},其中{a, b, c}没有上一个元素,{c, b, a}没有下一个元素。
    sort(arr,arr+n);
            int add = 0;
            do
            {
                if(arr[0] != 0)
                {
                 add++;
                
                }
            }
            while(next_permutation(arr,arr+n));
    它能够将要排列的数在一个数组的形式下一次循环,将要执行的操作在if语句里面完成就可以了!!!
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<string>
     5 #include<set>
     6 #include<vector>
     7 #include<stack>
     8 #include<queue>
     9 #include<algorithm>
    10 #include<iostream>
    11 #include<cstdio>
    12 #include<algorithm>
    13 using namespace std;
    14 int main()
    15 {
    16     int arr[1000];
    17     int n,m;
    18 
    19     while(cin>>n>>m)
    20     {
    21 
    22         for(int i=0;i<n;i++)
    23             arr[i]=i+1;
    24 
    25         sort(arr,arr+n);
    26         int add = 0;
    27         do
    28         {
    29             if(arr[0] != 0)
    30             {
    31              add++;
    32              if(add==m)
    33             {for(int i=0;i<n;i++)
    34              {
    35                  if(i==0)
    36                  cout<<arr[i];
    37                  else cout<<" "<<arr[i];
    38                  }
    39                  break;
    40                  }
    41             }
    42         }
    43         while(next_permutation(arr,arr+n));
    44         cout<<endl;
    45     }
    46 
    47 
    48     return 0;
    49 }
  • 相关阅读:
    Kibana
    Filebeat使用
    leetcode刷题笔记七十三题 矩阵置零
    leetcode刷题笔记七十二题 编辑距离
    leetcode刷题笔记七十一题 简化路径
    leetcode刷题笔记七十题 爬楼梯
    leetcode刷题笔记六十九题 X的平方根
    python 冒泡算法
    hive 函数
    Task07:类、对象与魔法方法(3天)
  • 原文地址:https://www.cnblogs.com/blvt/p/7202240.html
Copyright © 2011-2022 走看看