Smallest Difference

Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 5364
Accepted: 1456

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

Rocky Mountain 2005




题意:


由上面可知道,题目的大概的意思就是首先输入几组测试用例,
T
接着输入
一组不超过10的数据
例如
0 1 2 4 6 7
然后自己随意用这几个数字随机组合,成两个数字,
要求就是挑选出两组数字差值最小的两组的差是多少?

例如这里给的数字是  0 1 2 4 6 7
那么差值最小的组数是
204   176
那么差值就是28


解题分析:


对于这种题目来说解法一般就是搜索,
那么在这里的话,我的想法是先对每组输入的数据进行排序
并且对于输入的数一字符串的形式输入,
查看一共有多少个字符
接着当总数是偶数的时候可以分成两部分呢第一部分第一个数字是较大的话
那么他后面跟着的数字就较小的数字。
另一个数组是较小的数字,后面跟着的就是较大的数字。
例如
2 0 4       1   7  6
这组比例就可以说明我上面的解释。

代码:


#include <iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int main()
{
    char s[55];
    int a[15];
    int b[15];
    int T,n,m,i,j,x,y,ans,k;
    scanf("%d",&T);
    gets(s);
    while(T--)
    {
        gets(s);
        int num=0;
        for(i=0;i<strlen(s);i++)
        {
            if(s[i]>='0'&&s[i]<='9')a[num++]=s[i]-'0';
        }
        sort(a,a+num);
        if(num%2==0)
        {
            k=num/2;
            i=1;
            int tem=55,c=1;
            if(a[0]==0)i=2;
            for(i=i;i<num;i++)
            {
                if(tem>a[i]-a[i-1])
                {
                    tem=a[i]-a[i-1];
                    c=i;
                }
                else if(tem==a[i]-a[i-1])
                {
                    if(abs(c-k)>abs(i-k))c=i;
                }
            }
            x=a[c];y=a[c-1];
            j=0;
            for(i=0;i<num;i++)
            {
                if(a[i]==x||a[i]==y)continue;
                b[j++]=a[i];
            }
            k=(num-2)/2;
            for(i=0;i<k;i++)
                x=x*10+b[i];
            for(i=num-3;i>=k;i--)
                y=y*10+b[i];
            ans=x-y;
        }
        else
        {
            if(num==1)ans=a[0];
            else
            {
                k=(num+1)/2;
                if(a[0]==0)
                {
                    x=a[1]*10;
                    for(i=2;i<k;i++)
                        x=x*10+a[i];
                    y=0;
                    for(i=num-1;i>=k;i--)
                        y=y*10+a[i];
                    ans=x-y;
                }
                else
                {
                     x=0;
                    for(i=0;i<k;i++)
                        x=x*10+a[i];
                    y=0;
                    for(i=num-1;i>=k;i--)
                        y=y*10+a[i];
                    ans=x-y;
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}




你可能感兴趣的:(Smallest Difference)