【学习笔记】生成下一个排列(STL库函数next——permutation)

版权声明:转载请注明出处哦~ https://blog.csdn.net/Cassie_zkq/article/details/88046606

代码:

int main()
{
    int n,p[10];
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>p[i];
    sort(p,p+n);
    do{
        for(int i=0;i<n;i++)
            printf("%d ",p[i]);
        printf("\n");
    }while(next_permutation(p,p+n));
    return 0;
}

输出:

n=3,p[]={4,2,1}时

1 2 4 
1 4 2 
2 1 4 
2 4 1 
4 1 2 
4 2 1 

猜你喜欢

转载自blog.csdn.net/Cassie_zkq/article/details/88046606