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 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
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 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
这题并不难,我用的贪心,逻辑很容易就搞混了,重写了一遍终于AC了。
第一点,012的时候,不能是01 和2,0不能作为前导0,必须是10和2.两个数01的时候最小距离就是1;
第二点,输入数据是一个字符串,将字符串转化为数字。
然后就是分情况了,奇书和偶数的时候,当是奇数的时候,两个数相差最小的一定差一位数,比如5个数,最小的3个数从小到大组成的数和最大的2个数从大到下组成的数之差一定是最小的。
当时偶数的时候,两个数一定位数相同;然后将每两个数距离最小的单个数做最高位,比如说0124这4个数,01和21都差1,但是01不能用(0不能做前导0),只能是2和1做最高位,1做最高位,再从剩下的数0和4中找最大的4,就是14,2做最大的就找小的0,就是20,这样20和14之间就是6,即0124这四个数能组成的两个数之间最小距离是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&&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&&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;
}