POJ2718题解

原题再现

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

Source


题目大意

将一组数分为两组,分别组成两个新的数字(除非数字为零,否则不可以以零打头),求这两个数的差的绝对值最小可以为多少。

大体思路

枚举每组测试数据中所有元素的全排列,切割成a, b两个数,找到abs(a-b)的最小值

各函数功能说明

cut函数负责将传入的数组切成两个数,分别存入int &a 和 int &b 中,如果会切割出非法数字(指以零打头),则返回假。
bool cut(int arr[],int length,int cut_point,int &a,int &b)
{
	/*cut_point 在下标为几的元素后面切一刀*/
	
	if(arr[0]==0&&cut_point>0)return false;
	if(arr[cut_point+1]==0&&cut_point<length-2)return false;


	a=arr[0];
	for(int i=1; i<=cut_point; i++) {
		a=a*10+arr[i];
	}

	b=arr[cut_point+1];
	for(int i=cut_point+2; i<length; i++) {
		b=b*10+arr[i];
	}

	return true;
}
permutation函数负责枚举全排列,并 调用cut函数 在数组中央切一刀。因为要求的是a, b差值的最小值,所以a, b的位数大致相近,只要求在中央切一刀即可。求出a, b差值后,与全局变量smallest做比较(smallest初值为-1,如果此时smallest为初值则无条件覆盖),如果差值较小则更新smallest。
void permutation(int* arr,int length)
{
	int a,b;
	int temp;
	do {
		if(cut(arr,length,(length+1)/2-1,a,b)) {
			temp=abs(a-b);
			if(smallest==-1) {
				smallest=temp;
			} else {
				if(temp<smallest) {
					smallest=temp;
				}
			}
		}
	} while(next_permutation(arr,arr+length));
}

main函数负责基本的I/O。由于输入数据只告诉了我们有几组测试数据,而没有告诉我们一组测试数据中有几个数据。所以,必须以getline读入,再利用stringstream分割到数组中。需要注意的是:1. 执行cin>>int  和 getline(cin,string) 之间必须执行一次 cin.ignore(),否则读入会出问题(第一个getline会读入空数据);2. stringstream被用于分割数据前,必须用stringstream.clear()清空内容(一开始我忘记了这步操作,导致从第二组输入开始出现BUG)。

int main()
{
	int a[10] = {0};
	int n;//元素个数

	stringstream ss;
	string str;
	int cases;
	int i;


	cin>>cases;
	cin.ignore();
	while(cases--) {

		getline(cin,str);
		ss.clear();
		ss<<str;
		i=0;
		while(ss>>a[i]) {
			i++;
		}
		n=i;


		smallest=-1;
		permutation(a,n);
		cout<<smallest<<endl;
	}

	return 0;
}

完整代码

#include <iostream>
#include <cmath>
#include <sstream>
#include <algorithm>
using namespace std;

int smallest=-1;

bool cut(int arr[],int length,int cut_point,int &a,int &b)
{
	/*cut_point 在下标为几的元素后面切一刀*/
	
	if(arr[0]==0&&cut_point>0)return false;
	if(arr[cut_point+1]==0&&cut_point<length-2)return false;

//	stringstream ss;
//
//	for(int i=0; i<=cut_point; i++) {
//		ss<<arr[i];
//	}
//	ss>>a;
//	ss.clear();
//
//	for(int i=cut_point+1; i<length; i++) {
//		ss<<arr[i];
//	}
//	ss>>b;


	a=arr[0];
	for(int i=1; i<=cut_point; i++) {
		a=a*10+arr[i];
	}

	b=arr[cut_point+1];
	for(int i=cut_point+2; i<length; i++) {
		b=b*10+arr[i];
	}

	return true;
}

void permutation(int* arr,int length)
{
	int a,b;
	int temp;
	do {
		if(cut(arr,length,(length+1)/2-1,a,b)) {
			temp=abs(a-b);
			if(smallest==-1) {
				smallest=temp;
			} else {
				if(temp<smallest) {
					smallest=temp;
				}
			}
		}
	} while(next_permutation(arr,arr+length));
}

int main()
{
	int a[10] = {0};
	int n;//元素个数


	stringstream ss;
	string str;
	int cases;
	int i;


//	int arr[]= {1,2,9,3,0,5,6,7,8,4};
//	int c,d;
//	if(cut(arr,10,4,c,d))
//		cout<<c<<'\t'<<d<<endl;

	cin>>cases;
	cin.ignore();
	while(cases--) {

		getline(cin,str);
		ss.clear();
		ss<<str;
		i=0;
		while(ss>>a[i]) {
			i++;
		}
		n=i;

//		for(int k=0; k<n; k++) {
//			cout<<a[k]<<',';
//		}
//		cout<<endl;


		smallest=-1;
		permutation(a,n);
		cout<<smallest<<endl;
	}

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值