九个数的全排列(避免重复出现)
生活随笔
收集整理的這篇文章主要介紹了
九个数的全排列(避免重复出现)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
題意:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1384
?
使用標(biāo)準(zhǔn)庫(kù)里面的next_permutation()函數(shù),這個(gè)函數(shù)是生成所有比當(dāng)前的字符串大的字符串,所以最開(kāi)始經(jīng)過(guò)sort()排序之后,要先打印出當(dāng)前的字符串。
C++ STL中提供了std::next_permutation與std::prev_permutation可以獲取數(shù)字或者是字符的全排列,其中std::next_permutation提供升序、std::prev_permutation提供降序。
兩個(gè)代碼本質(zhì)是一樣的 ?一個(gè)從字符串出現(xiàn),一個(gè)從數(shù)組
#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);///將字符串按照字典數(shù)排序printf("%s\n", s);///生成所有比當(dāng)前的字符串大的字符串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;}
?
轉(zhuǎn)載于:https://www.cnblogs.com/a719525932/p/7839685.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的九个数的全排列(避免重复出现)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: PG数据库插件扩展搭建(一)
- 下一篇: 软件实施工程师到底是做什么的?