poj2718(Smallest Difference)

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

 

差值最小的两个数一定一个长度是n/2,先用一次DFS求第一个数,当DFS深度为n/2时,全排列枚举剩下的数。

#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <map>
#include <vector>
#include <list>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <limits>
#include <new>
#include <utility>
#include <iterator>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <ctime>
using namespace std;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);
int dx[] = {0, 1, 0, -1, 0}, dy[] = {-1, 0, 1, 0, 0};
const int maxn = 400;

int ans, n;
int num[10], vis[10];

void solve(int a)
{
    int tmp[10], cnt = 0;
    for (int i = 0; i < n; ++i)
        if (!vis[i])
            tmp[cnt++] = num[i];
    do
    {
        if (!tmp[0] && cnt > 1)
            continue;
        int b = tmp[0];
        for (int i = 1; i < cnt; ++i)
            b = b * 10 + tmp[i];
        ans = min(ans, abs(a-b));
    } while (next_permutation(tmp, tmp+cnt));
}

void dfs(int cur, int res)
{
    if (cur == n/2)
    {
        solve(res);
        return;
    }
    for (int i = 0; i < n; ++i)
        if (!vis[i])
        {
            if (!cur && !i && n > 2)
                continue;
            vis[i] = 1;
            dfs(cur+1, res*10+num[i]);
            vis[i] = 0;
        }
}

int main()
{
    int kase;
    cin >> kase;
    getchar();
    while (kase--)
    {
        ans = INF;
        memset(vis, 0, sizeof(vis));
        char ch;
        n = 0;
        while (scanf("%c", &ch) == 1 && ch != '\n')
            if (ch != ' ')
                num[n++] = ch - '0';
        dfs(0, 0);
        cout << ans << endl;
    }
    return 0;
}


 

算法码上来 文章被收录于专栏

公众号「算法码上来」。godweiyang带你学习算法,不管是编程算法,还是深度学习、自然语言处理算法都一网打尽,更有各种计算机新鲜知识和你分享。别急,算法码上来。

全部评论

相关推荐

10-14 23:01
已编辑
中国地质大学(武汉) Java
CUG芝士圈:虽然是网上的项目,但最好还是包装一下,然后现在大部分公司都在忙校招,十月底、十一月初会好找一些。最后,boss才沟通100家,别焦虑,我去年暑假找第一段实习的时候沟通了500➕才有面试,校友加油
点赞 评论 收藏
分享
11-08 13:58
门头沟学院 Java
程序员小白条:竟然是蓝桥杯人才doge,还要花钱申领的offer,这么好的公司哪里去找
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务