题意:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1384
使用标准库里面的next_permutation()函数,这个函数是生成所有比当前的字符串大的字符串,所以最开始经过sort()排序之后,要先打印出当前的字符串。
C++ STL中提供了std::next_permutation与std::prev_permutation可以获取数字或者是字符的全排列,其中std::next_permutation提供升序、std::prev_permutation提供降序。
两个代码本质是一样的 一个从字符串出现,一个从数组
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<queue> #include<map> #include<vector> #include<math.h> #include<string> #include<numeric> using namespace std; #define INF 0x3f3f3f3f #define LL long long #define N 106 #define Lson rood<<1 #define Rson rood<<1|1 int main() { char s[21]; scanf("%s", s); int lenth = strlen(s); std::sort(s, s + lenth);///将字符串按照字典数排序 printf("%s ", s);///生成所有比当前的字符串大的字符串 while (std::next_permutation(s, s + lenth)) printf("%s ",s); return 0; }
#include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> using namespace std; int main() { char s[10]; int a[10]; scanf("%s",s); for(int i=0; s[i]!='