51 nod 1384 next_permutation()函数

有时候感觉刷刷51也挺好的 确实能开阔一下自己的眼界

这道题是全排列的题

自己用swap写的,超烦,过了之后看了下大佬的代码,发现自己还是见识的少。。

next_permutation()是真的好用

#include<bits/stdc++.h>
using namespace std;
char arr[19];
int main()
{
    scanf("%s",arr);
    int len=strlen(arr);
    sort(arr,arr+len);
    char *s=arr,*t=arr+len;
    do
    {
        printf("%s\n",arr);
    }while(next_permutation(s,t));                                                                                        
}

next——permutation函数第一个参数是排列起始点,第二个参数排列终点,生成字典序大于上一个的字符串

能生成时返回true,否则返回false

所以这道题把字符串首先排序成字典序最小的状态,然后输出就好了。

于此相应,还有prev_permutation()

都是一样的,输出相反就是了。

posted @ 2018-08-31 16:14  pilium  阅读(76)  评论(0)    收藏  举报