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

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. int a[6],vis[5],t,sol[5],last;
  5. void dfs(int c)
  6. {
  7. if(c==5)
  8. {
  9. if(sol[1]==0)
  10. return; //千位是0的不符合
  11. if(t!=0 && sol[1]==last)
  12. printf(" "); //一行的最后一个数后面不能输出空格
  13. if(t!=0 && sol[1]!=last)
  14. {
  15. printf("\n"); //千位不同则换行
  16. }
  17. last=sol[1]; //记录上一个的千位
  18. int j=1;
  19. for(j; j<=4; j++)
  20. {
  21. printf("%d",sol[j]);
  22. }
  23. t++;
  24. }
  25. int i;
  26. for(i=1; i<=4; i++)
  27. {
  28. if(vis[i]==0)
  29. {
  30. vis[i]=1;
  31. sol[c]=a[i];
  32. dfs(c+1);
  33. vis[i]=0;
  34. while(a[i]==a[i+1]) //关键: 去重复
  35. i++; //因为题目的输入是从小到大(否则先排序),所以如果在一轮排列结束后
  36. } //如果后一个将要被选的数与上一轮这个下标的数相同,则跳过
  37. } //比如1 1 1 2,第一轮1 1 1 2,返回上一次dfs后sol[3]=2,sol[4]=1
  38. } //返回到cnt=2,此时a[2]==a[3]=1,若再选1则为1 1 1 2,跳过则为1 2 1 1
  39. int main()
  40. {
  41. int i,T=0;
  42. while(scanf("%d%d%d%d",&a[1],&a[2],&a[3],&a[4]))
  43. {
  44. if(a[1]==0&&a[2]==0&&a[3]==0&&a[4]==0) break;
  45. if(T!=0) printf("\n");
  46. T=1;
  47. memset(vis,0,sizeof(vis));/**vis(),记录那个数有没有用过**/
  48. t=0;
  49. dfs(1);
  50. printf("\n");
  51. }
  52. return 0;
  53. }
  1. #include <iostream>
  2. #include<stdio.h>
  3. #include<algorithm>
  4. #include<string.h>
  5. using namespace std;
  6. int main()
  7. {
  8. int a[5],i,j,k,m=0;
  9. while(~scanf("%d%d%d%d",&a[1],&a[2],&a[3],&a[4]))
  10. {
  11. if(a[1]==0&&a[2]==0&&a[3]==0&&a[4]==0)
  12. return 0;
  13. if(m)
  14. printf("\n");
  15. m=1;
  16. k=1;int h;
  17. do
  18. {
  19. if(a[1]==0)
  20. continue;
  21. if(k==1)
  22. {
  23. printf("%d%d%d%d",a[1],a[2],a[3],a[4]);
  24. k=0;
  25. }
  26. else
  27. {
  28. if(h==a[1])
  29. printf(" %d%d%d%d",a[1],a[2],a[3],a[4]);
  30. else
  31. printf("\n%d%d%d%d",a[1],a[2],a[3],a[4]);
  32. }
  33. h=a[1];
  34. }
  35. while(next_permutation(a+1,a+5));/**prev_permutation按降序排列,别忘了头文件#include<algorithm>**/
  36. printf("\n");
  37. }
  38. return 0;
  39. }
  1. next_permutation 需要头文件#include <algorithm>
  2. 这是一个求一个排序的下一个排列的函数,可以遍历全排列.
  3. next_permutation实现原理
  4. 在《STL源码解析》中找到了这个函数,在此也简单叙述一下原理:
  5. STL中,除了next_permutation外,
  6. 所谓“下一个”和“上一个”,书中举了一个简单的例子:对序列 {a, b, c},每一个元素都比后面的小,按照字典序列,固定a之后,abc都小,cb大,
  7. 它的下一个序列即为{a, c, b},而{a, c, b}的上一个序列即为{a, b, c},同理可以推出所有的六个序列为:
  8. {a, b, c}、{a, c, b}、{b, a, c}、{b, c, a}、{c, a, b}、{c,b, a},其中{a, b, c}没有上一个元素,{c, b, a}没有下一个元素。
  9. int 类型的next_permutation
  10. #include <iostream>
  11. #include <algorithm>
  12. using namespace std;
  13. int main()
  14. {
  15. int a[3];
  16. a[0]=1;
  17. a[1]=2;
  18. a[2]=3;
  19. do
  20. {
  21. cout<<a[0]<<" "<<a[1]<<" "<<a[2]<<endl;
  22. }
  23. while (next_permutation(a,a+3));//参数3指的是要进行排列的长度
  24. //如果存在a之后的排列,就返回true。如果a是最后一个排列没有后继,返回false,每执行一次,a就变成它的后继
  25. return 0;
  26. }
  27. 输出:
  28. 1 2 3
  29. 1 3 2
  30. 2 1 3
  31. 2 3 1
  32. 3 1 2
  33. 3 2 1
  34. 如果改成 while(next_permutation(a,a+2));
  35. 则输出:
  36. 1 2 3
  37. 2 1 3
  38. 只对前两个元素进行字典排序
  39. 显然,如果改成 while(next_permutation(a,a+1)); 则只输出:1 2 3
  40. 若排列本来就是最大的了没有后继,则next_permutation执行后,会对排列进行字典升序排序,相当于循环
  41. int list[3]={3,2,1};
  42. next_permutation(list,list+3);
  43. cout<<list[0]<<" "<<list[1]<<" "<<list[2]<<endl;
  44. //输出: 1 2 3

HDOJ 1716 排列2 next_permutation函数的更多相关文章

  1. hdu 1027 Ignatius and the Princess II(产生第m大的排列,next_permutation函数)

    题意:产生第m大的排列 思路:使用 next_permutation函数(头文件algorithm) #include<iostream> #include<stdio.h> ...

  2. HDOJ 1716 排列2(next_permutation函数)

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

  3. c++中STL中的next_permutation函数基本用法

    对于next_permutation函数是针对于排列组合问题的库函数,它的排序方式是按照字典的方式排列的·: 如以下代码对于next_permutation函数的初步解释: #include<c ...

  4. 全排列函数 nyoj 366(next_permutation()函数)

    C++ STL中提供了std::next_permutation与std::prev_permutation可以获取数字或者是字符的全排列,其中std::next_permutation提供升序.st ...

  5. POJ-1256 next_permutation函数应用

    字典序列: 在字典序中蕴含着一个点,就是大小的问题,谁先出现,谁后出现的问题.譬如a<b<c,出现顺序就是a,b,c. 本题中字符集是所有大小写字母,而题目中规定的谁大谁小已经不是按asc ...

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

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

  7. next_permutation()函数 和 prev_permutation() 按字典序求全排列

    next_permutation功能:    求一个排序的下一个排列的函数,可以遍历全排列,要包含头文件<algorithm> 与之完全相反的函数还有prev_permutation 这个 ...

  8. 【HDOJ】1716 排列2

    STL. /* 1716 */ #include <iostream> #include <algorithm> #include <cstdio> #includ ...

  9. 关于全排列 next_permutation() 函数的用法

    这是一个c++函数,包含在头文件<algorithm>里面,下面是基本格式. 1 int a[]; 2 do{ 3 4 }while(next_permutation(a,a+n)); 下 ...

随机推荐

  1. [DEncrypt] MySecurity--安全加密/Base64/文件加密 (转载)

    点击下载 MySecurity.zip 这个类是关于加密,解密的操作,文件的一些高级操作1.MySecurity  加密字符串2.MySecurity  加密字符串 密钥为系统默认 012345678 ...

  2. Android占位符

    <xliff:g>标签介绍: 属性id可以随便命名 属性值举例说明%n$ms:代表输出的是字符串,n代表是第几个参数,设置m的值可以在输出之前放置空格 %n$md:代表输出的是整数,n代表 ...

  3. linq按需查询

    将不确定变成确定~LINQ查询两种写法,性能没有影响,优化查询应该是“按需查询” 如果在linq中希望进行一对多的复合查询时,请直接在查询中使用join into,或者使用let 关键字,当然在建立实 ...

  4. CSS图片去色

    .imgFilter { filter: grayscale(100%); -webkit-filter: grayscale(100%); -moz-filter: grayscale(100%); ...

  5. 为什么喜欢Kindle

    为什么喜欢Kindle 有朋友问为什么那么多人喜欢用Kindle,作为刚入手一台Kindle PaperWhite 2 的人, 我想说:这个东西,应该算今年我买的最值得的设备了.它的好处太多了:1:轻 ...

  6. [转] CSS3混合模式mix-blend-mode/background-blend-mode简介 ---张鑫旭

    by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/wordpress/?p=4819 一.关于混合模 ...

  7. canonical 标签介绍

    rel=”canonical” 这个标签已经推出很久了,canonical 是 Google.雅虎.微软等搜索引擎一起推出的一个标签,它的主要作用是用来解决由于网址形式不同内容相同而造成的内容重复问题 ...

  8. Java控制台版推箱子

    import java.util.Scanner; public class b { public static void main(String[] args) { Scanner input = ...

  9. Centos7 修改运行级别

    systemd使用比sysvinit的运行级别更为自由的target概念作为替代 第三运行级: multi-user.target 第五运行级: graphical.target   #前者是符号链接 ...

  10. git 基础命令

    1.git init git 初始化仓库 2.git add . git 添加全部文件 3.git add xxx.txt   git 添加单独文件 4.git commit -m "提交的 ...