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. Markdown编辑器使用说明

    Markdown编辑器使用说明 #编辑器使用说明编辑器仅用来编辑文章的样式,建议在其他文档中写好内容,再复制到此处编辑样式--- ## 编辑器使用介绍—非开发者 非开发者,可以将编辑框内容全部删掉,使 ...

  2. 环绕声5.1ch

    简单说5.1ch就是数字影院中的音频输出术语,环绕立体声输出,让人有置身电影院的感觉,由五个音箱(两个主音箱.两个环绕箱.一个中置箱)+一个低音炮组成 5.1环绕声包括了5个全频带声道和 1个低频效果 ...

  3. Array和ArrayList不同

    Employee[] array = new Employee[10]; ArrayList<Employee> staff = new ArrayList<>(); 不同 A ...

  4. 怎样从外网访问内网WebSphere?

    本地安装了一个WebSphere,只能在局域网内访问,怎样从外网也能访问到本地的WebSphere呢?本文将介绍具体的实现步骤. 准备工作 安装并启动WebSphere 默认安装的WebSphere端 ...

  5. solr 使用指定数据源

    1,将solr 解压,我们观察发现它其实即可以是web服务也可以做数据分析 数据库 2,我们在example目录下新建一个hai的文件夹,用于存放数据 参考solr 目录,将solr.xml 复制一份 ...

  6. PHP 变量类型的强制转换 & 创建空对象

    PHP 在变量定义中不需要(或不支持)明示的类型定义:变量类型是根据使用该变量的上下文所决定的. 也就是说,如果把一个字符串值赋给变量 var,var 就成了一个字符串.如果又把一个整型值赋给 var ...

  7. P3809 【模板】后缀排序

    P3809 [模板]后缀排序 从这学的 后缀数组sa[i]就表示排名为i的后缀的起始位置 x[i]是第i个元素的第一关键字 y[i]表示第二关键字排名为i的数,在第一关键字中的位置 #include& ...

  8. JavaWeb中的资源映射

    一./与/* <url-pattern>/</url-pattern>  会匹配到/login这样的路径型url,不会匹配到模式为*.jsp这样的后缀型url< url- ...

  9. 20145212 罗天晨 MSF基础应用

    一.对exploit,payload,encode的理解 exploit是利用系统漏洞,相当于一个动态的过程. payload是攻击载荷,用于实现对漏洞的攻击,是实现漏洞攻击最核心的代码. encod ...

  10. 【TensorFlow】tf.nn.max_pool实现池化操作

    max pooling是CNN当中的最大值池化操作,其实用法和卷积很类似 有些地方可以从卷积去参考[TensorFlow]tf.nn.conv2d是怎样实现卷积的? tf.nn.max_pool(va ...