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
0 1 2 4 6 7

Sample Output

28

Source

题解:给一串0到9的数,选择几个组成num1,剩下的组成num2,问最小的差值,当有两个数字时不能0开始;暴力,先选数字然后全排列
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int ans;
int vis[];
int p[] = {,,,,,,,,,};
int get(int* a){
int i = ;
while(){
char c = getchar();
if(c == ' ')continue;
if(c == '\n')return i;
a[i ++] = c - '';
vis[c - ''] = ;
} } int cal(int *a, int n){
int x = ;
for(int i = ; i < n; i++){
// printf("%d ", a[i]);
x = x * + a[i];
}//printf("\n计算的结果是:\n", x);
return x;
} void distribute(int* a, int an, int* l, int ln, int* r, int rn, int i){ if(i == an){
// puts("l的元素是:");
// for(int j = 0; j < ln; j++){
// printf("%d ", l[j]);
// }puts("");
//
// puts("r的元素是:");
// for(int j = 0; j < rn; j++){
// printf("%d ", r[j]);
// }puts(""); if(ln == || rn == ){
return;
}
if(abs(rn - ln) > ){
return;
}
do{
do{
if(ln > && l[] == ){
continue;
}
if(rn > && r[] == ){
continue;
}
int x = cal(l, ln);
int y = cal(r, rn);
// printf("x = %d\ny = %d\n", x, y);
if(abs(x - y) <= ans){
ans = abs(x - y);
}
}while(next_permutation(r, r + rn));
}while(next_permutation(l, l + ln)); return;
} l[ln] = a[i];
distribute(a, an, l, ln + , r, rn, i + );
r[rn] = a[i];
distribute(a, an, l, ln, r, rn + , i + ); } int main(){
int T;
scanf("%d", &T);
getchar();
int a[], l[], r[];
while(T--){
memset(vis, , sizeof(vis));
int n = get(a);
ans = 0x3f3f3f3f;
// if(n == 10){
// puts("247");
// continue;
// }else if(n == 9){
// int m[10] = {2469,10469,469,369,359,358,359,369,469,1469};
// for(int i = 0; i < 10; i++){
// if(!vis[i]){
// printf("%d\n", m[i]);
// }
// }
// continue;
// }
distribute(a, n, l, , r, , );
printf("%d\n", ans);
}
return ;
}
 

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. s9303这样的arp表是代表什么意思?

    s9303这样的arp表是代表什么意思? 在s9303交换机下dis arp 看到了最末2条有这样的记录 那个Incomplete 是什么意思呢? 答: 如果该字段显示为“Incomplete”,表示 ...

  2. Linux文本编辑器(九)

    [教程主题]:Linux文本编辑器 [1]vi vi编辑器是所有Unix及Linux系统下标准的编辑器,它的强大不逊色于任 何最新的文本编辑器,这里只是简单地介绍一下它的用法和一小部分指令. 由于对U ...

  3. Kettle 添加对应hadoop版本的支持

    在hdp的官网上有一个ETL工具叫做Talend Open Studio,然后我就下了,并且在群里询问了一下,突然间冒出来一群ETL高手,经高人指点认识了一款叫做Kettle的软件,经过这两天的试用, ...

  4. java jdbc preparedstatement 分析

    https://blog.csdn.net/xiong9999/article/details/54137326

  5. Jackson 处理复杂类型(List,map)两种方法

    http://blog.csdn.net/zhuyijian135757/article/details/38269715 —————————————————————————————————————— ...

  6. 关于sentinel LDK加密war包实现应用加密的使用方法

    经过一周多时间的研究,终于对sentinel产品的使用有个基本的掌握.其中走了不少的弯路,特此记录一下,以备后面回顾. 开发前的用料准备: windows 10 x64位, tomcat7 x64bi ...

  7. Dijkstra 算法,用于对有权图进行搜索,找出图中两点的最短距离

    Dijkstra 算法,用于对有权图进行搜索,找出图中两点的最短距离,既不是DFS搜索,也不是BFS搜索. 把Dijkstra 算法应用于无权图,或者所有边的权都相等的图,Dijkstra 算法等同于 ...

  8. MYSQL数据库从A表把数据插入B表

    如果2张表的字段一致,并且希望插入全部数据,可以用这种方法: Code: INSERT INTO 目标表 SELECT * FROM 来源表; 比如要将 articles 表插入到 newArticl ...

  9. 锐捷 Fat/Fit Ap切换

    工作中要使用锐捷的AP和AC进行组网.记录一下RG-AP220-E配置成瘦AP的方法. 使用console口连接,baudrate rate: 9600, 8n1 瘦AP:console密码是ruij ...

  10. JS 在火狐浏览器下关闭弹窗

    1.首先,要确定火狐设置是否允许通过JS代码window.close()方法关闭标签. 确定方式如下:      在Firefox地址栏里输入 about:config    在配置列表中找到dom. ...