【POJ - 2718】Smallest Difference(搜索 )

-->Smallest Difference

直接写中文了

Descriptions:

给定若干位十进制数,你可以通过选择一个非空子集并以某种顺序构建一个数。剩余元素可以用相同规则构建第二个数。除非构造的数恰好为0,否则不能以0打头。 

举例来说,给定数字0,1,2,4,6与7,你可以写出10和2467。当然写法多样:210和764,204和176,等等。最后一对数差的绝对值为28,实际上没有其他对拥有更小的差。

Input 

输入第一行的数表示随后测试用例的数量。
对于每组测试用例,有一行至少两个不超过10的十进制数字。(十进制数字为0,1,…,9)每行输入中均无重复的数字。数字为升序给出,相隔恰好一个空格。

Output 

对于每组测试用例,输出一个以上述规则可获得的最小的差的绝对值在一行。

Sample Input 

1
0 1 2 4 6 7

Sample Output 

28

题目链接:

https://vjudge.net/problem/POJ-2718

 

先说题意

给你几个数字,可以分成两个子集,然后分别按一定顺序排列组成一个数,求出这两只值差的绝对值的最小值。设为n1,n2为两个新产生的数,那么n1,n2的位数越接近,其差的绝对值肯定越小

再者是排列了  题目说按一定顺序排列,那么这时候就可以用一个函数了(next_permutation(num,num+len))全排列函数 不会的可以参考 https://www.cnblogs.com/sky-stars/p/10948384.html 讲的很细

这两个问题解决就好写了

 

AC代码:

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define mod 1000000007
#define eps 1e-6
#define ll long long
#define INF 0x3f3f3f3f
#define MEM(x,y) memset(x,y,sizeof(x))
#define Maxn 1005
using namespace std;
int num[Maxn];
int n,len;//测试样例 数组长度
int ans;
void solve()
{
    while(n--)
    {
        len=0;//每次归0
        char ch;
        while(1)
        {
            scanf("%d%c",&num[len ++],&ch);//输入数据
            if (ch=='\n')//回车停止输入
                break;
        }
        if (len == 2)//就两个数
        {
            cout<<abs(num[0] - num[1])<<endl;
            continue;
        }
        int n1,n2;//两个重新组成的数
        ans=INF;
        int mid=len/2;//求最小值 肯定是数字位数越接近越小啦
        do
        {
            n1=num[0],n2=num[mid];
            if(n1==0||n2==0)
                continue;
            for(int i=1; i<mid; i++)//第一个重组数
                n1=n1*10+num[i];
            for(int i=mid+1; i<len; i++)//第二个重组数
                n2=n2*10+num[i];
            ans=min(ans,abs(n1-n2));//找最小值
        }
        while(next_permutation(num,num+len));//全排列
        cout<<ans<<endl;
    }
}
int main()
{
    cin>>n;
    solve();
}

 

posted on 2019-07-15 21:05  Sky丨Star  阅读(768)  评论(0编辑  收藏  举报

导航