POJ 2718 Smallest difference

Source: Internet
Author: User
Tags min time limit
Smallest difference
Time limit:1000ms Memory limit:65536k
Total submissions:5580 accepted:1509
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

1
0 1 2 4 6 7
Sample Output

28

The problem is not difficult, I use the greedy, logic is easy to confuse, rewrite it finally AC.

1th, 012, cannot be 01 and 2,0 cannot be a leading 0, must be 10 and 2. The minimum distance is two when 01 numbers are 1.

2nd, the input data is a string that converts the string to a number.

Then is the case, odd book and even when, when it is odd, the difference between the two number of the smallest difference of a number, such as 5 number, the smallest 3 numbers from small to large and the largest number of 2 numbers from the large to the bottom of the number must be the smallest.

Even at that time, two number is the same number of digits, and then the minimum number of each two is the smallest single number to do the highest bit, for example, 0124 of the 4 numbers, 01 and 21 are difference 1, but 01 can not be used (0 cannot do the leading 0), can only be 2 and 1 to do the highest bit, 1 to do the highest bit, and then Is the 14,2 do the largest to find a small 0, is 20, so 20 and 14 is 6, that 0124 of the four number can be composed of two number between the minimum distance is 6.


#include <iostream> #include <cstring> #include <cstdio> #include <cmath> #include <

Algorithm> using namespace std;
    int solve (int aa,int bb,int *a,int l,int len) {int sum=0;
    int NUMBER1=AA;
    int number2=bb;
            for (int i=len-1;i>=0;i--) {if (A[I]!=AA&AMP;&AMP;A[I]!=BB) {number1=number1*10+a[i];
        sum++;
    } if (sum==l) break;
    } sum=0;
          for (int i=0;i<len;i++) {if (A[I]!=AA&AMP;&AMP;A[I]!=BB) {number2=number2*10+a[i];
        sum++;
    } if (sum==l) break;
} return number2-number1;
       } void Print_ji (int *a,int l) {int mid=l/2;
       int number1=0;
       int number2=0;
           for (int i=0;i<=mid;i++) number1=number1*10+a[i];
           for (int i=l-1;i>mid;i--) number2=number2*10+a[i];
printf ("%d\n", number1-number2);
 } void Print_ou (int *a,int l) {   int MID=L/2;
   int minx=999;
       for (int i=1;i<l;i++) {if (a[i-1]==0) continue;
   Minx=min (Minx,a[i]-a[i-1]);
   } int minxx=999999; for (int i=1;i<l;i++) {if ((A[i]-a[i-1]) ==minx&&a[i-1]) {int different=solve (a[i-1],
          A[I],A,MID-1,L);
       Minxx=min (minxx,different);
}} printf ("%d\n", minxx);
    } int main (void) {///Freopen ("A.txt", "R", stdin);
    int n;
    scanf ("%d", &n);
    GetChar ();
        while (n--) {int a[50];
        memset (a,0,sizeof (0));
        Char s[50];
        Gets (s);
        int j=0;
                    for (int i=0;i<strlen (s); i++) {if (s[i]>= ' 0 ' &&s[i]<= ' 9 ') {
               a[j++]=s[i]-' 0 ';
        }} if (j==2) printf ("%d\n", ABS (A[0]-a[1]));
            else {sort (a,a+j); if (j%2) {if (a[0]==0) {a[0]=a[1];
                a[1]=0;
            } print_ji (A,J);
            } else {Print_ou (a,j);
}}} return 0;
 }

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.