十四、排列2
来源:互联网 发布:godaddy创建数据库 编辑:程序博客网 时间:2024/05/17 03:31
Problem Description
Ray又对数字的列产生了兴趣:
现有四张卡片,用这四张卡片能排列出很多不同的4位数,要求按从小到大的顺序输出这些4位数。
Input
每组数据占一行,代表四张卡片上的数字(0<=数字<=9),如果四张卡片都是0,则输入结束。
Output
对每组卡片按从小到大的顺序输出所有能由这四张卡片组成的4位数,千位数字相同的在同一行,同一行中每个四位数间用空格分隔。
每组输出数据间空一行,最后一组数据后面没有空行。
Sample Input
1 2 3 4
1 1 2 3
0 1 2 3
0 0 0 0
Sample Output
1234 1243 1324 1342 1423 1432
2134 2143 2314 2341 2413 2431
3124 3142 3214 3241 3412 3421
4123 4132 4213 4231 4312 4321
1123 1132 1213 1231 1312 1321
2113 2131 2311
3112 3121 3211
1023 1032 1203 1230 1302 1320
2013 2031 2103 2130 2301 2310
3012 3021 3102 3120 3201 3210
学习了一个函数permutation 全排序 和严格的格式输出;
next_permutation 只能对不重复的元素进行排序,如果对重复的元素进行全排序的话,需要添加限制条件;
比如ABAB 就打印不出AABB 来;
#include <iostream>#include <algorithm>using namespace std;int main(){ int t; int a[4]; int flag=1; while((cin>>a[0]>>a[1]>>a[2]>>a[3])&&(a[0]+a[1]+a[2]+a[3]!=0)) { if(!flag) cout<<endl; flag=1; do { if(a[0]==0) continue; if(flag) { cout<<a[0]<<a[1]<<a[2]<<a[3]; //每次都使用一次 flag = 0; } else if(a[0] == t) cout<<" "<<a[0]<<a[1]<<a[2]<<a[3]; else cout<<endl<<a[0]<<a[1]<<a[2]<<a[3]; t=a[0]; } while(next_permutation(a,a+4)); cout<<endl; } return 0;}
0 0
- 十四、排列2
- 排列2
- 排列2
- 排列2
- Java 欧拉工程 第二十四篇【0,1,2,3,4,5,6,7,8,9的第100万个字典排列是什么】
- 十四章上机2
- 十四
- 十四
- 十四
- hdu1716 排列2(排列生成算法)
- hdu 1716 排列2 (全排列)
- HDU1716 排列2【全排列+输出格式】
- (二十四)优秀员工 - 2
- 十四章(上机1,2)
- 十四章上机练习2
- AngualrJS(十四)$watch 2
- 课程二十四 项目2
- hdu1716排列2
- 微信小程序页面跳转传递值
- 104. Maximum Depth of Binary Tree
- 【APP推荐】手机制作微电影
- 在系统编程ISP及在应用编程IAP
- SpringAPI手动创建代理对象——ProxyFactory
- 十四、排列2
- 数据结构之顺序查找
- HTML5 canvas之旋转
- (计算几何)判断一个点是否在线段上
- django搭建个人博客08,添加访问权限
- 【bzoj2002】【Hnoi2010】【Bounce 弹飞绵羊】【lct】
- 排序 选择问题 Partition函数
- Spring学习笔记一
- 【C++】 LeetCode 98. Validate Binary Search Tree