Smallest Difference POJ 2718

1.题目描述:点击打开链接

2.解题思路:本题利用暴力搜索法解决。因为数据规模比较小,可以直接利用next_permutation函数枚举所有排列。根据经验知,当两个数的位数差不多时,差值可能达到最下。因此枚举完分两种情况分别计算差值并取较小者。(1)[0,cnt/2)和[cnt/2,cnt)。(2)[0,cnt/2+1)和[cnt/2+1,cnt)。其中cnt是数组的长度。考虑第二种情况是因为有0的存在。

3.代码:

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<string>
#include<sstream>
#include<set>
#include<vector>
#include<stack>
#include<map>
#include<queue>
#include<deque>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#include<functional>
using namespace std;

#define N 20
int digit[N];
#define INF 100000000
int ans;
int cal(int s, int e)
{
	int res = 0;
	for (int i = s; i < e; i++)
	{
		if (digit[s] == 0)break;
		res = 10 * res + digit[i];
	}
	return res == 0 ? INF : res;
}
int main()
{
	//freopen("t.txt", "r", stdin);
	int T;
	scanf("%d ", &T);
	while (T--)
	{
		string str;
		getline(cin, str);
		stringstream ss(str);
		ans = INF;
		int x, y, cnt = 0;
		while (ss >> x)
			digit[cnt++] = x;
		if (cnt == 2)ans = abs(digit[0] - digit[1]);
		else
		{
			do
			{
				x = cal(0, cnt >> 1);
				y = cal(cnt >> 1, cnt);
				if (x != INF&&y != INF)ans = min(ans, abs(x - y));
				x = cal(0, (cnt >> 1) + 1);
				y = cal((cnt >> 1) + 1, cnt);
				if (x != INF&&y != INF)ans = min(ans, abs(x - y));
			} while (next_permutation(digit, digit + cnt));
		}
		cout << ans << endl;
	}
	return 0;
}
时间: 2024-10-31 08:30:34

Smallest Difference POJ 2718的相关文章

Smallest Difference (poj 2718 暴力枚举)

Language: Default Smallest Difference Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5034   Accepted: 1382 Description Given a number of distinct decimal digits, you can form one integer by choosing a non-empty subset of these digits an

Smallest Difference POJ 2718(搜索)

原题 题目链接 题目分析 题目要求将一组数组成两个数,注意不能有前导零,要求差绝对值最小, 所以要取两个位数最接近的数来做差,然后搜就完事了.搜法可以用全排列搜,由于只取两个数,就可以直接取前half个数作为一个数,剩下的作为一个数,做差就行了,这样全排列刚好能遍历所有情况. 代码 1 #include <iostream> 2 #include <algorithm> 3 #include <utility> 4 #include <cstdio> 5 #

POJ 2718 Smallest Difference 未AC 《挑战程序设计竞赛》

题目:POJ 2718 思路: 分为奇数和偶数两种情况进行处理,输入个数为奇数的时候,无须穷举,最小差是一定的,如0 1 2 3 4,最小差为102 - 43. 输入个数为偶数的时候,用next_permutation穷举. 没有AC-- 1 #include <iostream> 2 #include <algorithm> 3 #include <iostream> 4 #include <stdio.h> 5 #include <string.h

POJ 2718 Smallest Difference(贪心 or next_permutation暴力枚举)

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

【POJ - 2718】Smallest Difference(搜索 )

-->Smallest Difference 直接写中文了 Descriptions: 给定若干位十进制数,你可以通过选择一个非空子集并以某种顺序构建一个数.剩余元素可以用相同规则构建第二个数.除非构造的数恰好为0,否则不能以0打头. 举例来说,给定数字0,1,2,4,6与7,你可以写出10和2467.当然写法多样:210和764,204和176,等等.最后一对数差的绝对值为28,实际上没有其他对拥有更小的差. Input  输入第一行的数表示随后测试用例的数量.对于每组测试用例,有一行至少两个

The Smallest Difference

Given two array of integers(the first array is array A, the second array is array B), now we are going to find a element in array A which is A[i], and another element in array B which is B[j], so that the difference between A[i] and B[j] (|A[i] - B[j

poj 2718 最优比例生成树(01分数规划)模板

/* 迭代法 :204Ms */ #include<stdio.h> #include<string.h> #include<math.h> #define N 1100 #define eps 1e-10 #define inf 0x3fffffff struct node { int u,v,w; }p[N]; double ma[N][N]; double distance(int i,int j) { return sqrt(1.0*(p[i].u-p[j].u

lintcode-medium-The Smallest Difference

Given two array of integers(the first array is array A, the second array is array B), now we are going to find a element in array A which is A[i], and another element in array B which is B[j], so that the difference between A[i] and B[j] (|A[i] - B[j

POJ2718 Smallest Difference 【贪心+枚举】

Smallest Difference Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4639   Accepted: 1290 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