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. Allegro Desgin Compare的用法与网表比较

    转:Allegro Desgin Compare的用法与网表比较 Allegro中自带有Design Compare工具,利用它可以比较明了的看到线路的差异.当然也可以通过SKILL进行比较,不过我们 ...

  2. Splash动画启动app时空白屏

    相信大多数人一开始都会对启动app的时候出现先白瓶或者黑屏然后才进入第一个界面,例如:SplashActivity.那这是什么原因造成的呢? <style name="Splash_T ...

  3. 开发ActiveX控件调用另一个ActiveX系列0——身份证识别仪驱动的问题

    程序员要从0下表开始,这篇是介绍这个系列的背景的,没有兴趣的人可以直接跳过. 为什么要开发ActiveX控件 由于工作需要,我们开发了一个网站,使用了一款身份证识别仪的网页ActiveX(OCX)插件 ...

  4. 前端标签--js--css大致思路

    html标签语言在块级和内联标签的基础上进行页面的设计,设计的时候主要是注意标签块间的距离位置等信息,设计盒子的浮动,盒子的位置,盒子之间的联系. 在设计网页之前一定要判断好该设计多少个盒子,什么样的 ...

  5. 安装 r 里的 igraph 报错

    转载来源:http://genek.tv/article/40 1186 0 0 安装 r 里的 igraph 报错: foreign-graphml.c: In function ‘igraph_w ...

  6. 通过srvctl add命令添加database信息到srvctl管理器

    ================================================通过srvctl add命令添加database信息到srvctl管理器================ ...

  7. php类中const

    常量 const 在类里面定义常量用 const 关键字,而不是通常的 define() 函数. 语法: const constant = "value"; 例子: <?ph ...

  8. POJ 3895 Cycles of Lanes (dfs)

    Description Each of the M lanes of the Park of Polytechnic University of Bucharest connects two of t ...

  9. Objective-C 内存管理之dealloc方法中变量释放处理

    本文转载至 http://blog.sina.com.cn/s/blog_a843a8850101ds8j.html   (一).关于nil http://cocoadevcentral.com/d/ ...

  10. UIWebView使用中的内存相关问题

    本文转载至: http://blog.csdn.net/musou_ldns/article/details/7675589   applicationios5webkit测试cacheios 在iO ...