POJ 2718 Smallest Difference(最小差)
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(最小差)的更多相关文章
- poj 2718 Smallest Difference(暴力搜索+STL+DFS)
Smallest Difference Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6493 Accepted: 17 ...
- POJ 2718 Smallest Difference dfs枚举两个数差最小
Smallest Difference Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19528 Accepted: 5 ...
- POJ 2718 Smallest Difference(贪心 or next_permutation暴力枚举)
Smallest Difference Description Given a number of distinct decimal digits, you can form one integer ...
- poj 2718 Smallest Difference(穷竭搜索dfs)
Description Given a number of distinct , the integer may not start with the digit . For example, , , ...
- 穷竭搜索: POJ 2718 Smallest Difference
题目:http://poj.org/problem?id=2718 题意: 就是输入N组数据,一组数据为,类似 [1 4 5 6 8 9]这样在0~9之间升序输入的数据,然后从这些数据中切一 ...
- POJ 2718 Smallest Difference 枚举
http://poj.org/problem?id=2718 题目大意: 给你一些数字(单个),不会重复出现且从小到大.他们可以组成两个各个位上的数字均不一样的数,如 0, 1, 2, 4, 6 ,7 ...
- POJ - 2718 Smallest Difference(全排列)
题意:将n个数字分成两组,两组分别组成一个数字,问两个数字的最小差值.要求,当组内数字个数多于1个时,组成的数字不允许有前导0.(2<=n<=10,每个数字范围是0~9) 分析: 1.枚举 ...
- POJ 2718 Smallest Difference【DFS】
题意: 就是说给你一些数,然后要求你使用这些数字组成2个数,然后求他们的差值最小. 思路: 我用的双重DFS做的,速度还比较快,其中有一个很重要的剪枝,若当前搜索的第二个数后面全部补零与第一个数所产生 ...
- POJ 2718 Smallest Difference(dfs,剪枝)
枚举两个排列以及有那些数字,用dfs比较灵活. dfs1是枚举长度短小的那个数字,dfs2会枚举到比较大的数字,然后我们希望低位数字的差尽量大, 后面最优全是0,如果全是0都没有当前ans小的话就剪掉 ...
随机推荐
- 解读NoSQL数据库的四大家族
在目前的企业IT架构中,系统管理员以及DBA都会考虑使用NoSQL数据库来解决RDBMS所不能解决的问题,特别是互联网行业.传统的关系型数据库主要以表(table)的形式来存储数据,而无法应对非结构化 ...
- python 使用json.dumps() 的indent 参数,获得漂亮的格式化字符串后输出
想获得漂亮的格式化字符串后输出,可以使用json.dumps() 的indent 参数.它会使得输出和pprint() 函数效果类似 >>> data {'age': 4, 'nam ...
- curl 命令简介
curl命令用于在命令行中发送HTTP请求: curl -i -H 'content-type: application/json' -X POST -d '{"name":&qu ...
- 在Hue中提交oozie定时任务
可以参见下面这篇博文: 通过hue提交oozie定时任务
- Web3.js API 中文文档
Web3.js API 中文文档 http://web3.tryblockchain.org/Web3.js-api-refrence.html web3对象提供了所有方法. 示例: //初始化过程 ...
- 第一周java测验感想
在正式开学的第一周,建民老师就给我们来了一个下马威.我本身的编程基础比较差,不知道怎么去想,怎么去一步步的去完成这么一个工程.所以我在星期四的下午十分的痛苦…因为不知道怎么搞嘛.尽管在暑假的时候看了 ...
- USB基础知识概论(版本:v0.9.2)
源: USB基础知识概论
- USB设备被识别流程
源: USB设备被识别流程
- django自定义错误响应
在做一个web时,总是会出现各种错误,如400.403.404.500等.一般开发都要做对应的处理,给一些友好提示,或返回一些公益广告等. 在Django中,默认提供了常见的错误处理方式,比如: ha ...
- Linux内核启动流程与模块机制
本文旨在简单的介绍一下Linux的启动流程与模块机制: Linux启动的C入口位于/Linux.2.6.22.6/init/main.c::start_kernel() 下图简要的描述了一下内核初始化 ...