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. ps存jpeg,格式保存的时候为什么选择“基线”

    jpeg是印前和网页设计常用的格式,最大好处就是能很大程度上压缩图像大小. 在ps中将图片保存为jpeg格式的时候会出现以下选项: 其中:图像选项都很熟悉,是关于图像质量的:而格式选项的用途主要是针对 ...

  2. Oracle静态监听和动态监听

    一.静态注册 静态注册指实例启动时读取listener.ora配置文件,将实例和服务注册到监听程序.无论何时启动一个数据库,默认都有两条信息注册到监听器中:实例和服务.SID_LIST_LISTENE ...

  3. Java BufferedWriter与BufferedReader操作文本文件

    /** * 采用字符流读取写入文本文件 */ public class FileUtil { /** * 写文件 * @param fileName * @param content */ publi ...

  4. Codeforces 366C Dima and Salad

    http://codeforces.com/problemset/problem/366/C 题意:在一个冰箱里有n种水果,并且这些水果每一种都有一个美味度和一个卡路里的属性, 小明要从这些水果中选出 ...

  5. Qt判断和打开进程(windows端),运行,检测,中止

    windows端的Qt程序往往需要打开外部程序(如:prog.exe),并且需要确定这个外部程序是唯一打开的. 1.判断系统中是否存在prog.exe void judge() { QProcess ...

  6. 读论文系列:Nearest Keyword Search in XML Documents中使用的数据结构(CT、ECT)

    Reference: [1]Y. Tao, S. Papadopoulos, C. Sheng, K. Stefanidis. Nearest Keyword Search in XML Docume ...

  7. jquery datepicker日期控件用法

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.c ...

  8. Controller 中Action 返回值类型 及其 页面跳转的用法

        •Controller 中Action 返回值类型 View – 返回  ViewResult,相当于返回一个View 页面. -------------------------------- ...

  9. c语言所有的errno枚举值含义

    可以通过以下代码,获取所有的错误码信息: #include <string.h> /* for strerror */ #include <errno.h> #include ...

  10. Unity Navigation面板了解

    上次讲解了下Navigation的简单使用, 这次来看看Navigation面板的一些参数 NavigationStatic 勾选后表示该对象参与导航网格的烘培. OffMeshLink Genera ...