hdu1716(库函数next_permutation)

来源:互联网 发布:链轮尺寸计算软件 编辑:程序博客网 时间:2024/05/13 05:10

题目意思:

现有四张卡片,用这四张卡片能排列出很多不同的4位数,要求按从小到大的顺序输出这些4位数。

注意首位没有前导0

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


题目分析:

库函数next_permutation()应用,直接调用库函数,输出时注意前导0,和空格,祥见代码


AC代码:


#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int main()
{
    int a[4],ok=0;
    cin>>a[0]>>a[1]>>a[2]>>a[3];
    while(1){
        if(a[0]+a[1]+a[2]+a[3]==0) break;
        sort(a,a+4);//排序
        int k=a[0];
        if(a[0]!=0) cout<<a[0]<<a[1]<<a[2]<<a[3];
        while(next_permutation(a,a+4)){
            if(a[0]==k&&a[0]!=0) cout<<" "<<a[0]<<a[1]<<a[2]<<a[3];
            else{
                if(a[0]!=0){
                    if(k!=0) cout<<endl;//换行
                    cout<<a[0]<<a[1]<<a[2]<<a[3];
                }
                k=a[0];
            }
        }
        cout<<endl;
        cin>>a[0]>>a[1]>>a[2]>>a[3];//只有下次不退出才换行
        if(a[0]+a[1]+a[2]+a[3]!=0) cout<<endl;
    }
    return 0;
}




1 0
原创粉丝点击