POJ 2718 Smallest Difference最小差

Time Limit: 1000MS    Memory Limit: 65536K

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.

给定若干位十进制数,你可以通过选择一个非空子集并以某种顺序构建一个数。剩余元素可以用相同规则构建第二个数。除非构造的数恰好为0,否则不能以0打头。

举例来说,给定数字0,,,,6与7,你可以写出10和2467。当然写法多样:210和764,204和176,等等。最后一对数差的绝对值为28,实际上没有其他对拥有更小的差。

CN

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.

输入第一行的数表示随后测试用例的数量。

对于每组测试用例,有一行至少两个不超过10的十进制数字。(十进制数字为0,,…,)每行输入中均无重复的数字。数字为升序给出,相隔恰好一个空格。

CN

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.

对于每组测试用例,输出一个以上述规则可获得的最小的差的绝对值在一行。

CN

Sample Input - 输入样例

1
0 1 2 4 6 7

Sample Output - 输出样例

28

题解

  对给定的数字任意组合,不得出现前导0,因此直接把划分位置选定为中间。枚举情况即可。

代码 C++

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
char data[];
int rd(int l, int r){//[l, r]
int opt = ;
for (; l <= r; ++l) opt = opt * + data[l] - '';
return opt;
}
int main(){
int t, i, j, mid, l, r, opt;
scanf("%d ", &t);
while (t--){
memset(data, , sizeof data); opt = 0x7FFFFFFF;
gets(data);
for (i = , j = ; data[i - ]; ++i, j += ) data[i] = data[j];
i -= ; mid = i >> ;
do {
if (mid && data[] == '') continue;
if (i - mid - && data[mid + ] == '') continue;
l = rd(, mid); r = rd(mid + , i);
opt = std::min(opt, std::abs(l - r));
} while (std::next_permutation(data, data + i + ));
printf("%d\n", opt);
}
return ;
}

POJ 2718 Smallest Difference(最小差)的更多相关文章

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

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

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

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

  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(穷竭搜索dfs)

    Description Given a number of distinct , the integer may not start with the digit . For example, , , ...

  5. 穷竭搜索: POJ 2718 Smallest Difference

    题目:http://poj.org/problem?id=2718 题意: 就是输入N组数据,一组数据为,类似 [1  4  5  6  8  9]这样在0~9之间升序输入的数据,然后从这些数据中切一 ...

  6. POJ 2718 Smallest Difference 枚举

    http://poj.org/problem?id=2718 题目大意: 给你一些数字(单个),不会重复出现且从小到大.他们可以组成两个各个位上的数字均不一样的数,如 0, 1, 2, 4, 6 ,7 ...

  7. POJ - 2718 Smallest Difference(全排列)

    题意:将n个数字分成两组,两组分别组成一个数字,问两个数字的最小差值.要求,当组内数字个数多于1个时,组成的数字不允许有前导0.(2<=n<=10,每个数字范围是0~9) 分析: 1.枚举 ...

  8. POJ 2718 Smallest Difference【DFS】

    题意: 就是说给你一些数,然后要求你使用这些数字组成2个数,然后求他们的差值最小. 思路: 我用的双重DFS做的,速度还比较快,其中有一个很重要的剪枝,若当前搜索的第二个数后面全部补零与第一个数所产生 ...

  9. POJ 2718 Smallest Difference(dfs,剪枝)

    枚举两个排列以及有那些数字,用dfs比较灵活. dfs1是枚举长度短小的那个数字,dfs2会枚举到比较大的数字,然后我们希望低位数字的差尽量大, 后面最优全是0,如果全是0都没有当前ans小的话就剪掉 ...

随机推荐

  1. Python: 字典dict: 相同点

    问题:怎么样在两个字典中找相同点 answer: eg1: 下面2个字典 a={'x':1,'y':2,'z':3},    b={'w':10,'x':11,'y':2}, 1)找相同点: a.ke ...

  2. 设计模式之Proxy(代理)(转)

    理解并使用设计模式,能够培养我们良好的面向对象编程习惯,同时在实际应用中,可以如鱼得水,享受游刃有余的乐趣. Proxy是比较有用途的一种模式,而且变种较多,应用场合覆盖从小结构到整个系统的大结构,P ...

  3. hive中的with用法

    hive 可以通过with查询来提高查询性能,因为先通过with语法将数据查询到内存,然后后面其它查询可以直接使用,这种方法与创建临时表类似但是不需要创建临时表实体表,内存中的子查询结果在会话结束后会 ...

  4. js异步计时器

    js中同步和异步的区别: 1.同步会阻塞代码执行,而异步不会 2.alert 是同步,setTimeout 是异步 何时需要异步 1.在可能发生等待的情况 2.等待过程中不能像 alert 一样阻塞程 ...

  5. [转载]PowerDesigner生成的ORACLE 建表脚本中去掉对象的双引号,设置大、小写

    若要将 CDM 中将 Entity的标识符都设为指定的大小写,则可以这么设定: 打开cdm的情况下,进入Tools-Model Options-Naming Convention,把Name和Code ...

  6. Spring Boot 实现RESTful webservice服务端实例

    1.Spring Boot configurations application.yml spring: profiles: active: dev mvc: favicon: enabled: fa ...

  7. mysql INSERT ... ON DUPLICATE KEY UPDATE语句

    网上关于INSERT ... ON DUPLICATE KEY UPDATE大多数文章都是同一篇文章转来转去,首先这个语法的目的是为了解决重复性,当数据库中存在某个记录时,执行这条语句会更新它,而不存 ...

  8. windows 10下oracle相关异常及处理方法

    话说起来,不以oracle性能优化,数据库维护为主业已经有四五年了,这两年基本上以mysql为主. pl/sql登录后提示空白对话框.将ORACLE_HOME设置为oracle 11g的目录. IMP ...

  9. mvc 遇到的问题

    VS2010无法加载项目,此安装不支持该项目类型. 错误产生的原因是以前是用2010建的,后来用2012打开,可能是经过转换后,2010又打不开了. 用VS2010无法加载项目,提示:无法打开项目文件 ...

  10. android官方开发教程解释(一)

    最近准备系统学一下android开发,这里不会照搬原文,只会针对教程中一些难以理解的部分进行解释,我只是个菜鸟. 在教程第一章——入门基础里面,讲解android主题的那个小节,大概会有以下的代码: ...