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

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

 
给定一个数字序列,将其分为两部分,求差绝对值的最小值;
next_permutation()即可,但需要判断首元素是否为0
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
int a[];
int main()
{
int T;
scanf("%d",&T);
getchar();
while(T--)
{
int temp;
int n=,i,Min=;
while((temp=getchar())!='\n')
{
if(temp!=' ')
a[n++]=temp-'';
}
sort(a,a+n);
/* for(i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;*/
if(n==&&a[]==)
{
cout<<a[]<<endl;
continue;
}
do{
int num1=,num2=,m;
if(a[]==||a[n/]==)
continue;
for(i=;i<n/;i++)
num1=num1*+a[i];
for(;i<n;i++)
num2=num2*+a[i];
Min=min(Min,abs(num1-num2));
//cout<<num1<<" "<<num2<<" "<<abs(num1-num2)<<" "<<Min<<endl;
}while(next_permutation(a,a+n));
printf("%d\n",Min);
}
}
 

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

  1. POJ 2718 Smallest Difference(最小差)

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

  2. 1038 Recover the Smallest Number (30 分)

    1038 Recover the Smallest Number (30 分) Given a collection of number segments, you are supposed to r ...

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

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

  4. 01背包问题:Charm Bracelet (POJ 3624)(外加一个常数的优化)

    Charm Bracelet    POJ 3624 就是一道典型的01背包问题: #include<iostream> #include<stdio.h> #include& ...

  5. Scout YYF I(POJ 3744)

    Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5565   Accepted: 1553 Descr ...

  6. 广大暑假训练1(poj 2488) A Knight's Journey 解题报告

    题目链接:http://vjudge.net/contest/view.action?cid=51369#problem/A   (A - Children of the Candy Corn) ht ...

  7. Games:取石子游戏(POJ 1067)

    取石子游戏 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 37662   Accepted: 12594 Descripti ...

  8. BFS 或 同余模定理(poj 1426)

    题目:Find The Multiple 题意:求给出的数的倍数,该倍数是只由 1与 0构成的10进制数. 思路:nonzero multiple  非零倍数  啊. 英语弱到爆炸,理解不了题意... ...

  9. 并查集+关系的传递(poj 1182)

    题目:食物链 题意:给定一些关系.判断关系的正确性,后给出的关系服从之前的关系: 思路:难点不在并查集,在于关系的判断,尤其是子节点与根节点的关系的判断: 这个关系看似没给出,但是给出子节点与父节点的 ...

随机推荐

  1. Largest Rectangle in a Histogram(HDU 1506 动态规划)

    Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  2. iOS: 消息通信中的Notification&KVO

    iOS: 消息通信中的Notification&KVO 在 iOS: MVC 中,我贴了张经典图: 其中的Model向Controller通信的Noification&KVO为何物呢? ...

  3. POJ 2986 A Triangle and a Circle

    题意:给定一个三角形,以及一个圆的圆心坐标和半径,求圆和三角形的相交面积. 思路: 用三角剖分,三角形上每个线段都变成这个线段与圆心的三角形,然后算出每个三角形与圆的相交面积,然后根据有向面积的正负累 ...

  4. poj3261 -- Milk Patterns

                                                                        Milk Patterns Time Limit: 5000MS ...

  5. 使用opencv传中文文件崩溃

    这个问题经过我的调试发现:   程序是在 while (*at && !isdigit(*at)) at++;   这个语句时crash的,但是跟进去是isdigit的问题,因为变量a ...

  6. Linux上ld和ld.so命令的区别

    显然,ld链接器,它的生命周期是发生在compile-time的,它的一些参数是编译时期gcc给传递的,比如,指定需要链接什么库. ld.so命令的周期是发生在run-time的,名字叫动态链接器/加 ...

  7. Could not open a connection to your authentication agent

    执行ssh-add ~/.ssh/rsa  就会遇到上述错误了 解决方案: 先执行  eval `ssh-agent`  (是-键上的那个`) 再执行 ssh-add ~/.ssh/rsa成功 ssh ...

  8. 10.1.5 Connection Character Sets and Collations

    10.1.5 Connection Character Sets and Collations Several character set and collation system variables ...

  9. .net 分页案例效果

     ;             )             {                 pageUrl += ) +  && (totalPageCount > step  ...

  10. mook_百度百科

    mook_百度百科 mook