九个数的全排列(避免重复出现)
生活随笔
收集整理的這篇文章主要介紹了
九个数的全排列(避免重复出现)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題意: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\n", s);///生成所有比當前的字符串大的字符串while (std::next_permutation(s, s + lenth))printf("%s\n",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]!='\0'; i++)a[i]=s[i]-'0';int l=strlen(s);sort(a,a+l);for(int i=0; i<l; i++)printf("%d",a[i]);printf("\n");while(next_permutation(a,a+l)){for(int i=0; i<l; i++)printf("%d",a[i]);printf("\n");}return 0;}
?
轉載于:https://www.cnblogs.com/a719525932/p/7839685.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的九个数的全排列(避免重复出现)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PG数据库插件扩展搭建(一)
- 下一篇: 软件实施工程师到底是做什么的?