HDU 1716 排序2

来源:互联网 发布:js获取省市区 编辑:程序博客网 时间:2024/06/18 07:08

这一题我开始用递归做得,但是格式我改不了(太菜了)

所以改用STL中的“下一个排列” next_permutation.

以后要在格式上下点功夫QAQ

#include<iostream>#include<algorithm>using namespace std;int main(){int sub[5],k=0;while(cin>>sub[0]>>sub[1]>>sub[2]>>sub[3],sub[0]||sub[1]||sub[2]||sub[3]){int tag=0;if(k++) cout<<endl;int temp;sort(sub,sub+4);int flag=1;do{if(sub[0]==0){   //temp=sub[1];   continue;}if(flag){cout<<sub[0]<<sub[1]<<sub[2]<<sub[3];flag=0;}else if(temp==sub[0]) {cout<<" ";for(int i=0;i<4;i++) cout<<sub[i];}else{cout<<endl;cout<<sub[0]<<sub[1]<<sub[2]<<sub[3];}  // printf("")temp=sub[0];}while(next_permutation(sub,sub+4)); cout<<endl;}return 0;}