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

题目要求的就是四个数全排列从小到大输出。

由于考虑到需要排序,而且要去重,所以直接使用了set容器。

全排列用dfs生成。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <set>
#define LL long long using namespace std; int a[];
bool vis[];
set <int> ans; void dfs(int now, int val)
{
if (now == )
{
if (val >= )
ans.insert(val);
return;
}
for (int i = ; i < ; ++i)
{
if (vis[i])
continue;
vis[i] = true;
dfs(now+, val*+a[i]);
vis[i] = false;
}
} void Work()
{
ans.clear();
memset(vis, , sizeof(vis));
dfs(, );
set <int>::iterator it;
int high = -;
bool flag = false;
for (it = ans.begin(); it != ans.end(); ++it)
{
if (high == -)
high = *it/;
if (high != - && *it/ != high)
{
printf("\n");
high = *it/;
flag = false;
}
if (flag)
printf(" ");
printf("%d", *it);
flag = true;
}
printf("\n");
} int main()
{
//freopen("test.in", "r", stdin);
bool flag = false;
while (scanf("%d%d%d%d", &a[], &a[], &a[], &a[]) != EOF &&
(a[]+a[]+a[]+a[]))
{
if (flag)
printf("\n");
Work();
flag = true;
}
return ;
}

ACM学习历程—HDU1716 排列2(dfs && set容器)的更多相关文章

  1. ACM学习历程—BZOJ 2115 Xor(dfs && 独立回路 && xor高斯消元)

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2115 题目大意是求一条从1到n的路径,使得路径xor和最大. 可以发现想枚举1到n的所有路 ...

  2. ACM学习历程—UESTC 1219 Ba Gua Zhen(dfs && 独立回路 && xor高斯消元)

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1219 题目大意是给了一张图,然后要求一个点通过路径回到这个点,使得xor和最大. 这是CCPC南阳站的一道题 ...

  3. ACM学习历程—SNNUOJ1215 矩阵2(二分 && dfs)

    http://219.244.176.199/JudgeOnline/problem.php?id=1215 这是这次微软和百度实习面试的一道题,题目大意就是:有一个n*m的矩阵,已知它每一行都是不严 ...

  4. ACM学习历程—Hihocoder 1291 Building in Sandbox(dfs && 离线 && 并查集)

    http://hihocoder.com/problemset/problem/1291 前几天比较忙,这次来补一下微软笔试的最后一题,这题是这次微软笔试的第四题,过的人比较少,我当时在调试B题,没时 ...

  5. ACM学习历程—HDU5269 ZYB loves Xor I(位运算 && dfs && 排序)(BestCoder Round #44 1002题)

    Problem Description Memphis loves xor very musch.Now he gets an array A.The length of A is n.Now he ...

  6. ACM学习历程—ZOJ 3861 Valid Pattern Lock(dfs)

    Description Pattern lock security is generally used in Android handsets instead of a password. The p ...

  7. ACM学习历程——HDU5202 Rikka with string(dfs,回文字符串)

    Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he ...

  8. 完成了C++作业,本博客现在开始全面记录acm学习历程,真正的acm之路,现在开始

    以下以目前遇到题目开始记录,按发布时间排序 ACM之递推递归 ACM之数学题 拓扑排序 ACM之最短路径做题笔记与记录 STL学习笔记不(定期更新) 八皇后问题解题报告

  9. ACM学习历程—BestCoder Round #75

    1001:King's Cake(数论) http://acm.hdu.edu.cn/showproblem.php?pid=5640 这题有点辗转相除的意思.基本没有什么坑点. 代码: #inclu ...

随机推荐

  1. 为什么阿里巴巴不建议在for循环中使用"+"进行字符串拼接

    字符串,是Java中最常用的一个数据类型了.关于字符串的知识,作者已经发表过几篇文章介绍过很多,如: Java 7 源码学习系列(一)--String 该如何创建字符串,使用" " ...

  2. Spring Cloud Zuul API服务网关之请求路由

    目录 一.Zuul 介绍 二.构建Spring Cloud Zuul网关 构建网关 请求路由 请求过滤 三.路由详解 一.Zuul 介绍 ​ 通过前几篇文章的介绍,我们了解了Spring Cloud ...

  3. static修饰内部类

    创建内容类的方式通过外部类的实例对象来创建 public class AA { int a =1; class BB { int b=3 ; } public static void main(Str ...

  4. go with go

    1, vim 安装vim-go 打造GOLANG 专用IDE golang和vim-go安装配置 2, 阅读图书 <Go语言实战> William Kennedy等, 李兆海 译 3,在线 ...

  5. ASP.NET MVC4+BootStrap 实战(一)

    好久没有写关于web开发的文章了,进到这个公司一直就是winform和Silverlight,实在是没有实战web项目的机会.大D也辞职了,去搞web app了.自己也该闲暇时间多学习学习,每天进步一 ...

  6. python 基础 8.5 re 的match对象

    #/usr/bin/python #coding=utf-8 #@Time   :2017/11/18 21:49 #@Auther :liuzhenchuan #@File   :match对象.p ...

  7. python 基础 1.4 python运算符

    一. 布尔值: 1>True 2>False       二.关系运算符 “=” (a=b):把b的值赋给a.等号赋值   “==”(a==b): 判断a与b是否相等.返回Trule或Fl ...

  8. 通用分页(Jquery版)

    1.简单定义下样式 <style type="text/css"> .fanye { color: blue; margin-right: 15px; text-dec ...

  9. android菜鸟学习笔记15----Android Junit测试

    Android中的Junit测试与Java Junit测试有所不同,不能简单的使用标注…… 假设写了一个MathUtils类,有两个静态方法: public class MathUtils { pub ...

  10. Learning an Optimal Policy: Model-free Methods

    http://www.mit.edu/~9.54/fall14/slides/Reinforcement%20Learning%202-Model%20Free.pdf [基于所有.单个样本]