HDU 1716 - 排列2

知識點:全排列函數 next_permutation, );

當然,暴力也可以解決。

#include <cstdio> 
#include <cstdlib>             
#include <algorithm>    //包含於頭文件<algorithm>

using namespace std;

inline int cmp(const void *a,const void *b)
{
    return *(int *)a-*(int *)b;
}

int main()
{
    int a[4]; 
    bool flag=0; 
    while(scanf("%d%d%d%d",&a[0],&a[1],&a[2],&a[3])!=EOF)
    {
        if(!a[0]&&!a[1]&&!a[2]&&!a[3]) break;
        if(flag) putchar('\n');
        qsort(a,4,sizeof(int),cmp);
        while(a[0]==0) next_permutation(a,a+4);    //next_permutation( ,)的用法
        int a0=a[0],a1=a[1],a2=a[2],a3=a[3],tmp=a[0];
        while(1)
        {
            printf("%d%d%d%d",a[0],a[1],a[2],a[3]);
            next_permutation(a,a+4);
            if((a[0]==a0&&a[1]==a1&&a[2]==a2&&a[3]==a3)||a[0]==0) break;
            if(a[0]==tmp) putchar(' ');
            else
            {
                tmp=a[0];
                putchar('\n');
            }
        }
        putchar('\n');
        flag=1;
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章