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.
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带你学习算法,不管是编程算法,还是深度学习、自然语言处理算法都一网打尽,更有各种计算机新鲜知识和你分享。别急,算法码上来。