【POJ】最小差值

来源:互联网 发布:装修效果图设计软件 编辑:程序博客网 时间:2024/06/05 00:23

【POJ】最小差值

题目

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

解析

1.手写排列

#include<cstdio>#include<iostream>#include<algorithm>using namespace std;string s1;int a[15],Min,p,n;int ABS(int a){    return a<0?-a:a;}void swap(int &x,int &y){    int t = x;x = y;y = t;}void Fun(int x){    if( x == n )    {        int A = 0,B = 0;        if( a[1] != 0 && a[p] != 0 )        {            for(int j=1;j<p;j++)                A = A * 10 + a[j];            for(int j=p;j<=n;j++)                B = B * 10 + a[j];            if( Min > ABS(A-B) ) Min = ABS(A-B);        }        return ;    }    for(int i=x;i<=n;i++)    {        swap(a[x],a[i]);        Fun(x+1);        swap(a[x],a[i]);    }}int main(){    int Case;    scanf("%d\n",&Case);    for(int i=1;i<=Case;i++)    {        getline(cin,s1);        n = 0;        for(int j=0;j<int(s1.size());)        {            a[++n] = s1[j] - '0';            j = j + 2;        }        sort(a+1,a+n+1);        Min = 10000000;p = (n+1)/2+1;        Fun(1);        if( n == 2 ) printf("%d\n",ABS(a[1]-a[2]));        else printf("%d\n",Min);    }}

2. next_permutation#include

#include<iostream>#include<algorithm>using namespace std;string s1;int a[15];int ABS(int a){    return a<0?-a:a;}int main(){    int Case;    scanf("%d\n",&Case);    for(int i=1;i<=Case;i++)    {        getline(cin,s1);        int n = 0;        for(int j=0;j<int(s1.size());)        {            a[++n] = s1[j] - '0';            j = j + 2;        }        sort(a+1,a+n+1);        int Min = 10000000,p = (n+1)/2+1;        do        {            int A = 0,B = 0;            if( a[1] != 0 && a[p] != 0 )            {                for(int j=1;j<p;j++)                    A = A * 10 + a[j];                for(int j=p;j<=n;j++)                    B = B * 10 + a[j];                if( Min > ABS(A-B) ) Min = ABS(A-B);            }        }while(next_permutation(a+1,a+n+1));        if( n == 2 ) printf("%d\n",ABS(a[1]-a[2]));        else printf("%d\n",Min);    }}

3. 贪心

#include<cstdio>#include<algorithm>#include<iostream>using namespace std;string s1;int a[15];int main(){    int Case;    scanf("%d\n",&Case);    for(int i=1;i<=Case;i++)    {        getline(cin,s1);        int n = 0;        for(int j=0;j<int(s1.size());)        {            a[++n] = s1[j] - '0';            j = j + 2;        }        sort(a+1,a+n+1);        int A = 0,B = 0;        if( n % 2 == 1 )        {            if( a[1] == 0 )            {                A = a[2]*10 + a[1];                for(int j=3;j<=n/2+1;j++)                    A = A * 10 + a[j];            }            else             {                A = a[1];                for(int j=2;j<=n/2+1;j++)                    A = A * 10 + a[j];            }            B = a[n];            for(int j=n-1;j>n/2+1;j--)                B = B * 10 + a[j];            printf("%d\n",A-B);        }        else        {            int Min = 10000;            for(int i=1;i<n;i++)            {                if( a[i] == 0 ) continue;                A = a[i+1];B = a[i];                int s = 1,t = n;                while( s <= t )                {                    while( s == i || s == i+1 ) s++;                    while( t == i || t == i+1 ) t--;                    if( s <= t ) A = A * 10 + a[s];                    if( s <= t ) B = B * 10 + a[t];                    s++;t--;                }                if( A - B < Min ) Min = A - B;            }            if( n == 2 ) printf("%d\n",a[2] - a[1]);            else printf("%d\n",Min);        }    }}