Smallest Difference
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10387   Accepted: 2836

Description

Given a number of distinct decimal digits, you can form one integer by choosing a non-empty subset of these digits and writing them in some order. The remaining digits can be written down in some order to form a second integer. Unless the resulting integer is 0, the integer may not start with the digit 0.

For example, if you are given the digits 0, 1, 2, 4, 6 and 7, you can write the pair of integers 10 and 2467. Of course, there are many ways to form such pairs of integers: 210 and 764, 204 and 176, etc. The absolute value of the difference between the integers in the last pair is 28, and it turns out that no other pair formed by the rules above can achieve a smaller difference.

Input

The first line of input contains the number of cases to follow. For each case, there is one line of input containing at least two but no more than 10 decimal digits. (The decimal digits are 0, 1, ..., 9.) No digit appears more than once in one line of the input. The digits will appear in increasing order, separated by exactly one blank space.

Output

For each test case, write on a single line the smallest absolute difference of two integers that can be written from the given digits as described by the rules above.

Sample Input

  1. 1
  2. 0 1 2 4 6 7

Sample Output

  1. 28

Source

题解:给一串0到9的数,选择几个组成num1,剩下的组成num2,问最小的差值,当有两个数字时不能0开始;暴力,先选数字然后全排列
代码:
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. using namespace std;
  6. int ans;
  7. int vis[];
  8. int p[] = {,,,,,,,,,};
  9. int get(int* a){
  10. int i = ;
  11. while(){
  12. char c = getchar();
  13. if(c == ' ')continue;
  14. if(c == '\n')return i;
  15. a[i ++] = c - '';
  16. vis[c - ''] = ;
  17. }
  18.  
  19. }
  20.  
  21. int cal(int *a, int n){
  22. int x = ;
  23. for(int i = ; i < n; i++){
  24. // printf("%d ", a[i]);
  25. x = x * + a[i];
  26. }//printf("\n计算的结果是:\n", x);
  27. return x;
  28. }
  29.  
  30. void distribute(int* a, int an, int* l, int ln, int* r, int rn, int i){
  31.  
  32. if(i == an){
  33. // puts("l的元素是:");
  34. // for(int j = 0; j < ln; j++){
  35. // printf("%d ", l[j]);
  36. // }puts("");
  37. //
  38. // puts("r的元素是:");
  39. // for(int j = 0; j < rn; j++){
  40. // printf("%d ", r[j]);
  41. // }puts("");
  42.  
  43. if(ln == || rn == ){
  44. return;
  45. }
  46. if(abs(rn - ln) > ){
  47. return;
  48. }
  49. do{
  50. do{
  51. if(ln > && l[] == ){
  52. continue;
  53. }
  54. if(rn > && r[] == ){
  55. continue;
  56. }
  57. int x = cal(l, ln);
  58. int y = cal(r, rn);
  59. // printf("x = %d\ny = %d\n", x, y);
  60. if(abs(x - y) <= ans){
  61. ans = abs(x - y);
  62. }
  63. }while(next_permutation(r, r + rn));
  64. }while(next_permutation(l, l + ln));
  65.  
  66. return;
  67. }
  68.  
  69. l[ln] = a[i];
  70. distribute(a, an, l, ln + , r, rn, i + );
  71. r[rn] = a[i];
  72. distribute(a, an, l, ln, r, rn + , i + );
  73.  
  74. }
  75.  
  76. int main(){
  77. int T;
  78. scanf("%d", &T);
  79. getchar();
  80. int a[], l[], r[];
  81. while(T--){
  82. memset(vis, , sizeof(vis));
  83. int n = get(a);
  84. ans = 0x3f3f3f3f;
  85. // if(n == 10){
  86. // puts("247");
  87. // continue;
  88. // }else if(n == 9){
  89. // int m[10] = {2469,10469,469,369,359,358,359,369,469,1469};
  90. // for(int i = 0; i < 10; i++){
  91. // if(!vis[i]){
  92. // printf("%d\n", m[i]);
  93. // }
  94. // }
  95. // continue;
  96. // }
  97. distribute(a, n, l, , r, , );
  98. printf("%d\n", ans);
  99. }
  100. return ;
  101. }
 

Smallest Difference(暴力全排列)的更多相关文章

  1. poj 2718 Smallest Difference(暴力搜索+STL+DFS)

    Smallest Difference Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6493   Accepted: 17 ...

  2. POJ2718Smallest Difference(暴力全排列)

    传送门 题目大意:升序输入十进制数 没有重复 分成两个非空集合 每个集合组成一个数(不能有前导零) 求两个数差的最小值. 题解:全排列...我数组从1开始怎么一直WA...还有这个输入值得学习. 代码 ...

  3. POJ 2718 Smallest Difference(贪心 or next_permutation暴力枚举)

    Smallest Difference Description Given a number of distinct decimal digits, you can form one integer ...

  4. 【POJ - 2718】Smallest Difference(搜索 )

    -->Smallest Difference 直接写中文了 Descriptions: 给定若干位十进制数,你可以通过选择一个非空子集并以某种顺序构建一个数.剩余元素可以用相同规则构建第二个数. ...

  5. POJ 2718 Smallest Difference dfs枚举两个数差最小

    Smallest Difference Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19528   Accepted: 5 ...

  6. LintCode "The Smallest Difference"

    Binary search. class Solution { int _findClosest(vector<int> &A, int v) { , e = A.size() - ...

  7. Smallest Difference(POJ 2718)

    Smallest Difference Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6740   Accepted: 18 ...

  8. POJ 2718 Smallest Difference(最小差)

     Smallest Difference(最小差) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 Given a numb ...

  9. The Smallest Difference

    Given two array of integers(the first array is array A, the second array is arrayB), now we are goin ...

随机推荐

  1. android开发(31) 动画演示 - 从页面底部向上弹出dialog,消失时逐渐向下

    我想实现一个效果,从底部向上逐渐弹出.如下图所示: 1.点击 显示 按钮时,一个dialog对话框从底部慢慢向上弹出. 2.关闭dialog时, dialog缓慢的移动向底部消失.很平滑的效果.   ...

  2. 【javascript】js 检验密码强度

    最近一直在做通行证项目,里面的注册模块中输入密码需要显示密码强度(低中高).今天就把做的效果给大家分享下,代码没有网上搜索的那么复杂,能够满足一般的需求. html 代码如下: <!DOCTYP ...

  3. Entity Framework应用:根据实体的EntityState状态实现增删改查

    在上一篇文章中,我们讲解了使用EF实现简单的增删改成,在这篇文章中我们使用实体的EntityState状态来优化数据的增删改查. 一.修改数据 上篇文章中的修改数据的方法是EF官方推荐的方式,即先查询 ...

  4. 在kali linux之下 下载并解压的文件名呈现乱码 解决方案

    从Linux往 windows拷贝文件或者从windows往Linux拷贝文件,有时会出现中文文件名乱码的情况,出现这种问题的原因是因为,windows的文件名中文编码默认为GBK,而Linux中默认 ...

  5. Ubuntu之网易云音乐无法启动

    官方最新版(1.1)有这个问题,似乎改成0.9版就可以了 deepin15(32位):http://s1.music.126.net/download/pc/netease-cloud-music_0 ...

  6. 【5】JVM-垃圾收集器

    通过学习了解到现在商用的JVM中的垃圾收集采用的是分代收集算法,即针对不同年代采用不同的收集算法.在JVM中,GC主要作用于堆内存中,堆内存又被划分为新生代和老年代,由于新生代对象绝大多数是朝生夕死, ...

  7. 【转】【WPF】WPF MVVM 简单实例

    1 新建WPF 应用程序WPFMVVMExample 程序结构如下图所示. 2 Model实现 在Model文件夹下新建业务类StudentModel(类文件StudentModel.cs),类的详细 ...

  8. 第三百三十一节,web爬虫讲解2—Scrapy框架爬虫—Scrapy安装—Scrapy指令

    第三百三十一节,web爬虫讲解2—Scrapy框架爬虫—Scrapy安装—Scrapy指令 Scrapy框架安装 1.首先,终端执行命令升级pip: python -m pip install --u ...

  9. SpringMVC系列(四)使用 POJO 对象绑定请求参数值

    在实际开发中如果参数太多就不能使用@RequestParam去一个一个的映射了,需要定义一个实体参数对象(POJO)来映射请求参数.Spring MVC 会按请求参数名和 POJO 属性名进行自动匹配 ...

  10. 详解SQLServer如何链接远程MySQL数据库

    最近遇到“SQL如何链接远程MySQL”这个问题,现在问题终于解决,特把方法贴出来:(我所用的操作系统是Win7,数据库是SQL2005.) 1.在SQL SERVER服务器上安装MYSQL ODBC ...