next_permutation

来源:互联网 发布:photoshopcs6 for mac 编辑:程序博客网 时间:2024/05/20 05:09

输入


aAb 
abc

输出

Aab
Aba
aAb
abA
bAa
baA
abc
acb
bac
bca
cab
cba
#include<stdio.h>#include<iostream>#include<algorithm>#include<string.h>using namespace std;int comp(char a,char b){    if(tolower(a)!=tolower(b))        return tolower(a)<tolower(b);    else        return a<b;}int main(){    char ch[20];    int n;    scanf("%d",&n);    while(n--)    {        scanf("%s",ch);        sort(ch,ch+strlen(ch),comp);        do        {            printf("%s\n",ch);        }        while(next_permutation(ch,ch+strlen(ch)));    }    return 0;}
aed
ade
aed
dae
dea
ead
eda
#
#include<stdio.h>#include<string>#include<iostream>#include<algorithm>using namespace std;int main(){    string line;    while(cin>>line)    {        if(line=="#")            break;        sort(line.begin(),line.end());        do        {            cout<<line<<endl;        } while(next_permutation(line.begin(),line.end()));    }    return 0;}
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
#include<stdio.h>#include<string>#include<iostream>#include<algorithm>using namespace std;int main(){    int a[3];    a[0]=1;    a[1]=2;    a[2]=3;    do    {        printf("%d %d %d\n",a[0],a[1],a[2]);    }    while(next_permutation(a,a+3));    return 0;}

1 2 3
2 1 3
#include<stdio.h>#include<string>#include<iostream>#include<algorithm>using namespace std;int main(){    int a[3];    a[0]=1;    a[1]=2;    a[2]=3;    do    {        printf("%d %d %d\n",a[0],a[1],a[2]);    }    while(next_permutation(a,a+2));//2指要进行排列的长度,只排列a[0]和a[1],a[3]不进行排序    return 0;}

2 1 3
#include<stdio.h>#include<string>#include<iostream>#include<algorithm>using namespace std;int main(){    int a[3];    a[0]=1;    a[1]=2;    a[2]=3;    int t=0;    do    {        t++;        if(t==3)//只输出第三组数据        printf("%d %d %d\n",a[0],a[1],a[2]);    }    while(next_permutation(a,a+3));    return 0;}



0 0
原创粉丝点击