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. jquery-1.10.2 获取checkbox的checked属性总是undefined

    项目中用的jquery-1.10.2 需要检测一个checkbox的选中状态,想当然的用 .attr("checked") ,结果发现,无论是否选中,这个值都是 undefined ...

  2. iOS 证书与签名 解惑详解

    iOS 证书与签名 解惑详解 分类: iPhone2012-06-06 19:57 9426人阅读 评论(1) 收藏 举报 iosxcodecryptographyappleiphone测试   目录 ...

  3. [原]Sublime Text3 搭建16位汇编环境(windows)

    最近在学习王爽的<汇编程序>,参考<简单OS开发前奏<一>EDITPLUS+MASM32搭建汇编开发环境(16位+32位)>http://www.cnblogs.c ...

  4. Log4net 配置详解

    首先需在config文件的<configSections>节点中增加子节点配置. <configSections> <section name="log4net ...

  5. 【温故而知新:文件操作】C#的文件读写相关

    StreamReader类以及其方法ReadLine,Read,ReadToEnd的分析 首先StreamReader类的构造参数非常丰富在这里,我觉得最常用的就是StreamReader(Strea ...

  6. rsyslog input 不支持变量

    input(type="imfile" File="/usr/local/apache-tomcat-7.0.55_8082/logs/localhost_access_ ...

  7. poj 2288 Islands and Bridges_状态压缩dp_哈密尔顿回路问题

    题目链接 题目描述:哈密尔顿路问题.n个点,每一个点有权值,设哈密尔顿路为 C1C2...Cn,Ci的权值为Vi,一条哈密尔顿路的值分为三部分计算: 1.每一个点的权值之和 2.对于图中的每一条CiC ...

  8. wireshark使用心得

    关于pcap文件的文件解析网上资料有很多,我在这就不说明了 心得一:wireshark Runtime Error 一般来说,wireshark不适合长时间捕获包,也就是随着时间增长,总会报出上述错误 ...

  9. 如何在cmd窗口启动Tomcat

    平时,一般使用tomcat/bin/startup.bat目录在windows环境启动Tomcat,或者使用IDE配置后启动. 下面来简单介绍下如果在cmd窗口直接输入命令启动Tomcat: 1.将t ...

  10. kafka与Spring的集成

    准备工作 kafka版本:kafka_2.10-0.10.1.0 spring版本:spring4.3 配置文件 pom文件配置(也可以直接下载jar包) Kafka和spring集成的支持类库,sp ...