Smallest difference (POJ 2718 Violent enumeration)

Source: Internet
Author: User

Language:DefaultSmallest difference
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5034 Accepted: 1382

Description

Given a number of distinct decimal digits, you can form one integer by choosing a non-empty subset of these digits and WRI Ting them in some order. The remaining digits can is written down in some order to form a second integer. Unless the resulting integer is 0, the integer could not be start with the digit 0.

For example, if is given the digits 0, 1, 2, 4, 6 and 7, you can write the pair of integers and 2467. Of course, there is 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 a, and it turns out this no other pair form Ed 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 are one line of input containing at least and no more than decimal digits. (The decimal digits is 0, 1, ..., 9.) No Digit appears more than once on one line of the input. The digits would appear in increasing order, separated by exactly one blank space.

Output

For each test case, write to a single line the smallest absolute difference of both integers that can is written from the G Iven digits as described by the rules above.

Sample Input

10 1 2 4 6 7

Sample Output

28

Source

Rocky Mountain 2005

Test instructions: Give 2~10 a different number, asking to divide these numbers into two piles, each of which can be combined into a number (the beginning cannot be 0), and the minimum value of two number difference.

Idea: Data is small, direct violence enumerates all the cases, and it is easy to think that the number of two digits should be as equal as possible to minimize the difference; the Next_permutation function is used when enumerating permutations.

Code:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < cmath> #include <string> #include <map> #include <stack> #include <vector> #include <set > #include <queue> #pragma comment (linker, "/stack:102400000,102400000") #define MAXN 1005#define MAXN 2005# Define mod 1000000009#define INF 0x3f3f3f3f#define pi ACOs ( -1.0) #define EPS 1e-6#define Lson rt<<1,l,mid#define RSO N rt<<1|1,mid+1,r#define FRE (i,a,b) for (i = A, I <= b; i++) #define FRL (i,a,b) for (i = A; I < b; i++) #define Mem (T, v) memset ((t), V, sizeof (t)) #define SF (n) scanf ("%d", &n) #define SFF (A, b) scanf ("%d%d", &a, & AMP;B) #define SFFF (a,b,c) scanf ("%d%d%d", &a, &b, &c) #define PF printf#define DBG pf ("hi\n"    typedef long Long ll;using namespace Std;int num[12];int main () {int i,j;    int cas,cnt;    Char ch;    SF (CAS);    GetChar ();        while (cas--) {cnt=0;while ((Ch=getchar ()) = ' \ n ') if (ch>= ' 0 ' &&ch<= ' 9 ') num[cnt++]=ch-' 0 ';            if (cnt==2) {pf ("%d\n", ABS (Num[1]-num[0]));        Continue        } int ans=inf;        while (num[0]==0) next_permutation (num,num+cnt);            do{int mid= (cnt+1) >>1;                if (Num[mid]) {int x=0,y=0;                FRL (I,0,mid) x=x*10+num[i];                FRL (i,mid,cnt) y=y*10+num[i];            Ans=min (Ans,abs (x-y));        }}while (Next_permutation (num,num+cnt));    PF ("%d\n", ans); } return 0;}



Smallest difference (POJ 2718 Violent enumeration)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.