【杭电oj】1716 - 排列2(字典序输出)
2016-03-08 23:19
197 查看
排列2
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6241 Accepted Submission(s): 2404
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
Source
2007省赛集训队练习赛(2)
学长教的函数好好用啊,就是这道题格式好坑爹,热点速度太慢,不多说看代码理解吧。
其实我还是希望可以学学用dfs的。
代码如下:
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int main()
{
int a[5],num=0;
while(~scanf ("%d %d %d %d",&a[0],&a[1],&a[2],&a[3]) && (a[0] || a[1] || a[2] || a[3]))
{
if(num) //这道题格式好坑爹啊
printf ("\n");
num++;
int flag=1,t;
do
{
if(a[0]==0)
continue;
if(flag)
{
printf ("%d%d%d%d",a[0],a[1],a[2],a[3]);
flag = 0; //第一个数字输入
}
else if(a[0] == t)
{
printf (" %d%d%d%d",a[0],a[1],a[2],a[3]);
}
else
{
printf ("\n%d%d%d%d",a[0],a[1],a[2],a[3]); //换了千位数,需要换行
}
t=a[0];
}
while(next_permutation(a,a+4));
printf ("\n");
}
return 0;
}
相关文章推荐
- Linux普通用户使用1024以下端口的问题
- 构建可枚举类型(IEnumerable和IEnumerator接口)
- Gulp实现web服务器
- 蓝桥杯:剪格子
- 高性能MySQL主从复制
- 杭电2089~不要62
- 【Eclipse】1、eclipse软件下载
- 正则--match
- 正则表达式----search
- 一种更清晰的 Android 架构
- [转]考虑 PHP 5.0~5.6 各版本兼容性的 cURL 文件上传
- 5-3 逆序的三位数
- BZOJ 1008 越狱
- Jquery插件
- tomcat启动后访问应用超时问题
- kali windows双系统安装!
- 冒泡排序
- iOS_XCode7_Launch Image 的初使用
- 安装php过程中的错误和解决方式 configure: error: jpeglib.h not found
- 软件工程