1. 程式人生 > >hdu1716(庫函數next_permutation)

hdu1716(庫函數next_permutation)

style break else div popu ext std rac ack

題目意思:

現有四張卡片,用這四張卡片能排列出非常多不同的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;
}




hdu1716(庫函數next_permutation)