题意很简单,只是有几个细节要注意,首先就是一次只是输入四个数字、输出结果要从小到大(进行全排列之前要进行排序)、题目要求千位数相同的在一行,中间使用空格隔开(第二次在输出的时候判断上一次记录的千位数是不是和这一次的相等,相等就输出空格,不等就输出换行)、每组输出数据间加一个空行,最后一组后不可以有空行(第二次之后输入的不是0 0 0 0就输出一个空行就可以了,否则break)。

  还要两个隐藏的条件:①输出的时候前导0的一组不要;②输入数据可以有相同的数字,输出的时候要进行去重;

  对于本题来说,由于时间复杂度不是很大,可以使用一个容器(vector,arraylist)把结果存起来,在存的时候或者是输出的时候去遍历整个容器是不是有当前的这组数据,再进行相关处理。

  我这里偷了一下懒(实际上是想熟悉一下set),不得说set确实挺好用的。全排列的知识点请参考:(默认从小到大的顺序储存)

  http://www.cnblogs.com/Ddlm2wxm/p/6637122.html

   具体输出格式以及代码控制见代码:

 1 #include <iostream>
2 #include <algorithm>
3 #include <string>
4 #include <cstring>
5 #include <cstdio>
6 #include <set>
7 using namespace std;
8
9 char s[8];
10 char res[8];
11 bool used[10];
12 char c;
13 bool f;
14 set<string> se;
15
16 void print () {
17
18 for (set<string>::iterator p = se.begin(); p != se.end(); ++p) {
19 if (f) {
20 if (c == (*p)[0]) {
21 cout << ' ';
22 } else {
23 cout << endl;
24 }
25 }
26 while ((*p)[0] == '0') {
27 p++;
28 }
29 cout << *p;
30 c = (*p)[0];
31 f = true;
32 }
33 }
34
35 void perm (int pos, int n) {
36 if (pos == n) {
37 se.insert (res);
38 return ;
39 }
40 for (int i = 0; i < n; ++i) {
41 if (!used[i]) {
42 res[pos] = s[i];
43 used[i] = true;
44 perm (pos+1, n);
45 used[i] = false;
46 }
47 }
48 }
49
50 int main () {
51 bool a = false;
52 while (1) {
53 f = false;
54 for (int i = 0; i < 4; ++i) {
55 cin >> s[i];
56 }
57 if (s[0] == '0' && s[1] == '0' && s[2] == '0' && s[3] == '0') {
58 break;
59 }
60 if (a) {
61 cout << endl;
62 }
63 a = true;
64 sort (s, s+4);
65 memset(used, false, sizeof (used));
66 perm(0, 4);
67 print();
68 se.clear();
69 cout << endl;
70 }
71 return 0;
72 }

  在这里添加一点使用set求交集、并集、差集的几个函数的笔记:

  

 1 #include <iostream>
2 #include <algorithm>
3 #include <set>
4 #include <cstdio>
5 using namespace std;
6
7 int main () {
8 set<int> a, b, c;
9 int n;
10 cout << "A:" << endl;
11 for (int i = 0; i < 3; ++i) {
12 cin >> n;
13 a.insert (n);
14 }
15 cout << "B:" << endl;
16 for (int i = 0; i < 4; ++i) {
17 cin >> n;
18 b.insert (n);
19 }
20
21 c.clear();
22 set_intersection (a.begin(), a.end(), b.begin(), b.end(), inserter(c, c.begin()));
23 for (set<int>::iterator p = c.begin(); p != c.end(); ++p) {
24 cout << *p << ' ';
25 }
26 cout << endl;
27
28 c.clear();
29 set_union (a.begin(), a.end(), b.begin(), b.end(), inserter(c, c.begin()));
30 for (set<int>::iterator p = c.begin(); p != c.end(); ++p) {
31 cout << *p << ' ';
32 }
33 cout << endl;
34
35 c.clear();
36 set_difference (a.begin(), a.end(), b.begin(), b.end(), inserter(c, c.begin()));
37 for (set<int>::iterator p = c.begin(); p != c.end(); ++p) {
38 cout << *p << ' ';
39 }
40 cout << endl;
41 return 0;
42 }
43
44 /*
45 A:
46 1 2 4
47 B:
48 3 2 1 3
49 1 2
50 1 2 3 4
51 4
52 */

  C++对学习算法来说,确实很方便,一方面节省了很多时间,另一方面也会忘记最基础算法的实现,比如想到交换,都可以一行swap(a, b);实现,都不会想到去另外定义一个变量t,更不会想到定义一个函数,使用指针的方式从地址上实现数值的交换。

  所以,先定个小目标:STL既要会用,更要学会看源代码。

hdu - 1716 排列2 (使用set对全排列结果去重)的更多相关文章

  1. hdu 1716 排列2(DFS搜索)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1716 排列2 Time Limit: 1000/1000 MS (Java/Others)    Me ...

  2. hdu 1716 排列

    题目 这道题是全排列问题,主要注意的是格式问题.觉得下面这种写法最为巧妙 #include <cstdio> #include <iostream> #include < ...

  3. HDU - 1716 排列2 水题

    排列2 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  4. HDU 1716 排列2

    排列2 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  5. 杭电的题,输出格式卡的很严。HDU 1716 排列2

    题很简单,一开始写代码,是用整数的格式写的,怎么跑都不对,就以为算法错了,去看大佬们的算法STL全排列:next_permutation(); 又双叒叕写了好几遍,PE了将近次,直到跑了大佬代码发现, ...

  6. HDU 1716 排列2 (格式问题+排列)

    题意:. 析:我们完全可以STL里面的函数next_permutation(),然后方便,又简单,这个题坑就是在格式上. 行末不能有空格,结尾不能有空行,不大好控制,必须控制好第一次数. 这个题本应该 ...

  7. hdu 1716(dfs)

    题目链接 : http://acm.hdu.edu.cn/showproblem.php?pid=1716     排列2   Problem Description Ray又对数字的列产生了兴趣:现 ...

  8. STL训练 HDU - 1716 Ray又对数字的列产生了兴趣:

    HDU - 1716 Ray又对数字的列产生了兴趣: 现有四张卡片,用这四张卡片能排列出很多不同的4位数,要求按从小到大的顺序输出这些4位数. Input 每组数据占一行,代表四张卡片上的数字(0&l ...

  9. HDU 1716:排列2(全排列)

    排列2 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

随机推荐

  1. 在go中通过cmd调用python命令行参数量级过大问题解决

    问题描述如下: 在go中使用cmd调用python命令行 cmd := exec.Command("python", "dimine/Kriging/matrix.py& ...

  2. 攻防世界 reverse easy_Maze

    easy_Maze 从题目可得知是简单的迷宫问题 int __cdecl main(int argc, const char **argv, const char **envp) { __int64 ...

  3. 开源的 Switch 模拟器——GitHub 热点速览 v.21.12

    作者:HelloGitHub-小鱼干 脸滚键盘操作选手小鱼干这里要推荐一个超酷 Switch 模拟器,不能埋没你的游戏天赋.Ryujinx 是一个 C# 写的 Switch 模拟器,1700+ 游戏可 ...

  4. 网络对抗技术Exp2-后门原理与实践

    后门概念 后门就是不经过正常认证流程而访问系统的通道. 哪里有后门呢? 编译器留后门 操作系统留后门 最常见的当然还是应用程序中留后门 还有就是潜伏于操作系统中或伪装为特定应用的专用后门程序. 下面是 ...

  5. Hi3559AV100 NNIE开发(7) Ruyistudio 输出mobileface_func.wk与板载运行mobileface_chip.wk输出中间层数据对比

    前面随笔讲了关于NNIE的整个开发流程,并给出了Hi3559AV100 NNIE开发(5)mobilefacenet.wk仿真成功量化及与CNN_convert_bin_and_print_featu ...

  6. DB性能瓶颈分析思路

    在性能分析过程中,经常遇到性能瓶颈出现在SQL的情况,此类问题通常可以分为两大类场景,一是SQL自身性能差导致的慢,如索引缺失.索引失效.统计信息不准确.SQL过于复杂等:二是由于外部原因等待导致的S ...

  7. Java开发工程师面试-基础

    JDK.JRE.JVM有什么区别? JDK:Java Development Kit 针对Java程序员的产品 JRE:Java Runtime Environment是运行Java的环境集合 JVM ...

  8. 文字变图片——GitHub 热点速览 v.21.14

    作者:HelloGitHub-小鱼干 程序的力量,在 deep-daze 体现得淋漓尽致,你用一句话描述下你的图片需求,它就能帮你生成对应图片.同样的,appsmith 的力量在于你只要拖拽即可得到一 ...

  9. var=value?export前后差在哪?-- Shell十三问<第五问>

    var=value?export前后差在哪?-- Shell十三问<第五问> 这次让我们暂时丢开 command line ,先来了解一下 bash 变量(variable)吧.所谓的 变 ...

  10. Broken Keyboard (a.k.a. Beiju Text) UVA - 11988

    You're typing a long text with a broken keyboard. Well it's not so badly broken. The only problem wi ...