首页 > 代码库 > hdu 1716 排列2(stl next_permutation)

hdu 1716 排列2(stl next_permutation)

http://acm.hdu.edu.cn/showproblem.php?pid=1716

 

考到题目直接套 next_permutation 没有注意到0不能为首位 结果wa了一整天

输出结构也略有些小坑

 

技术分享
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<cmath>using namespace std;int num[10];int main(){    int mark=0;    while(scanf("%d%d%d%d",&num[0],&num[1],&num[2],&num[3])!=EOF)    {        if(num[0]==0&&num[1]==0&&num[2]==0&&num[3]==0)  break;        if(mark==1) printf("\n");        if(mark==0) mark++;                sort(num,num+4);        int first,i;        while(num[0]==0)        {            next_permutation(num,num+4);        }        first=num[0];        int flag=0;        do        {            if(first!=num[0])            {                    first=num[0];                    flag=0;                    printf("\n");            }            if(first==num[0]&&flag!=0)            {                printf(" ");            }            for(int i=0;i<4;i++) printf("%d",num[i]);            flag++;        }while(next_permutation(num,num+4));        printf("\n");    }    return 0;}
View Code

 

离线处理

技术分享
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<cmath>using namespace std;int num[10000][10];int main(){    int coun=0;    while(1)    {        scanf("%d%d%d%d",&num[coun][0],&num[coun][1],&num[coun][2],&num[coun][3]);        if(num[coun][0]==0&&num[coun][1]==0&&num[coun][2]==0&&num[coun][3]==0)        break;        coun++;    }    for(int k=0;k<coun;k++)    {        sort(num[k],num[k]+4);        int first,i;        while(num[k][0]==0)        {            next_permutation(num[k],num[k]+4);        }        first=num[k][0];        int flag=0;        do        {            if(first!=num[k][0])            {                    first=num[k][0];                    flag=0;                    printf("\n");            }            if(first==num[k][0]&&flag!=0)            {                printf(" ");            }            for(int i=0;i<4;i++) printf("%d",num[k][i]);            flag++;        }while(next_permutation(num[k],num[k]+4));        printf("\n");        if(k!=coun-1)            printf("\n");    }    return 0;}
View Code

 

hdu 1716 排列2(stl next_permutation)