最小差值

来源:互联网 发布:七龙珠战斗力知乎 编辑:程序博客网 时间:2024/06/11 00:15

  • Title Description
  • Input
  • Output
  • Sample Input
  • Sample Output
  • 函数做法
  • 手写排列
  • 贪心
  • 代码

Title 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

10 1 2 4 6 7

Sample Output

28

这道题是一道十分基础的题。
大意是给出几组数,求将每组数分成两个数求其最小差值。(无前导0)
对于此题我们有3种做法

函数做法

do{}while(next_permutation(a+1,a+l+1));

这是一个十分牛逼的排列函数。
只需在while中判断是否差值最小就行了。

手写排列

思路同上。
但不用函数去排列了,而是自己手写排列。
下面代码为一个交换排序法。

void oswap(int i,int j){int t=a[i];a[i]=a[j];a[j]=t;}int aobs(int x){if(x>=0)return x;return -x;}void shu(){    if(a[1]&&a[(l+1)/2+1])    {        int r1=0,r2=0;        for(int i=1;i<=(l+1)/2;i++)            r1=r1*10+a[i];        for(int i=(l+1)/2+1;i<=l;i++)            r2=r2*10+a[i];        if(aobs(r1-r2)<ans)ans=aobs(r1-r2);    }}void dfs(int x){    if(x==l){shu();return ;}    for(int i=x;i<=l;i++)    {        oswap(x,i);        dfs(x+1);        oswap(x,i);    }}

贪心

当我们的数位为奇数时,我们只需要让数位长的数最小,另一个数最大。
而为偶数时,就有些复杂了。进行一个枚举作为靠中数,再依次加。
下列代码为复数形式时。

    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;    }

代码

函数排序

#include<cstdio>#include<algorithm>#include<cmath>using namespace std;//调用STL函数int a[1005],l,k;char c[1005];void shu(){    sort(a+1,a+l+1);    int Min=10000000,p=(l+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<=l;j++)                B=B*10+a[j];            if(Min>abs(A-B))                Min=abs(A-B);        }    }while(next_permutation(a+1,a+l+1));    if(l==2)printf("%d\n",abs(a[1]-a[2]));    else printf("%d\n",Min);}int main(){    scanf("%d\n",&k);    for(int kk=1;kk<=k;kk++)    {        gets(c);l=0;        for(int i=0;i<strlen(c);)        {a[++l]=c[i]-'0';i+=2;}        shu();    }}

手写排列

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int a[1005],l,k,ans;char c[1005];void oswap(int i,int j){int t=a[i];a[i]=a[j];a[j]=t;}int aobs(int x){if(x>=0)return x;return -x;}void shu(){    if(a[1]&&a[(l+1)/2+1])    {        int r1=0,r2=0;        for(int i=1;i<=(l+1)/2;i++)            r1=r1*10+a[i];        for(int i=(l+1)/2+1;i<=l;i++)            r2=r2*10+a[i];        if(aobs(r1-r2)<ans)ans=aobs(r1-r2);    }}void dfs(int x){    if(x==l){shu();return ;}    for(int i=x;i<=l;i++)    {        oswap(x,i);        dfs(x+1);        oswap(x,i);    }}int main(){    scanf("%d\n",&k);    for(int kk=1;kk<=k;kk++)    {        gets(c);l=0;ans=10000000;        for(int i=0;i<strlen(c);)        {a[++l]=c[i]-'0';i+=2;}        sort(a+0,a+l+1);if(l!=2)dfs(1);        else ans=aobs(a[2]-a[1]);        printf("%d\n",ans);    }}
原创粉丝点击