POJ:2718 Smallest Difference(暴力枚举)

Smallest Difference
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 10416 Accepted: 2845

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


题目大意:给你一些数字,这些数字都是0~9的数,且只会出现一次,让你随意组合这些数,组合成两个数,使得两数之差的绝对值最小,输出最小差的绝对值。(组合出的数的前导不能是0)
解题思路:求两数之差的最小值,如果所给数字的个数为偶数,那么肯定是n/2位的一个数减去n/2位的一个数所得的差才能尽量少,如果是奇数个数字,比如5个数字,那么结果肯定是一个三位数与一个二位数之差,用next_permutation函数枚举这些组合,然后更新结果即可。
代码如下:
#include <cstdio>
#include <cstring>
#include <cmath> 
#include <algorithm>
using namespace std;
#define MAX 0x3f3f3f3f
int a[15];
int ans,n,mid;
void slove()
{
	ans=MAX;
	sort(a,a+n);
	mid=(n+1)/2;
	while(a[0]==0)//把排列组合跑到a[0]不为0的那种,即第一个数x的前导一定不为0了 
	{
		next_permutation(a,a+n);
	}
	do
	{
		if(a[mid]!=0)//如果第二个数的前导也不为0 
		{
			int x=a[0],y=a[mid];
            for(int i=1;i<mid;i++)
            {
            	x=x*10+a[i];
			}
            for(int i=mid+1;i<n;i++)
            {
            	y=y*10+a[i];
			}
            if(ans>abs(x-y))
            {
            	ans=abs(x-y);
			}
		}
	}
	while(next_permutation(a,a+n));
	printf("%d\n",ans);
}
int main()
{
	int t;
	char c;
	scanf("%d",&t);
	getchar();//得放到这里,不能放到while循环内部,否则会出现第二组数据答案不照,不知道什么原理 
	while(t--)
	{
		memset(a,0,sizeof(a));
		n=0;
		while((c=getchar())!='\n')//新学的输入,也可以用gets()获得一个含空格的字符串,扫一遍放入数字数组也行 
        {
            if(c!=' ')
                a[n++]=c-'0';
        }
		if(n==2)
		{
			ans=abs(a[0]-a[1]);
			printf("%d\n",ans);
			continue;
		}
		else
		{
			slove();
		}
	}
	return 0;
} 


内容概要:本文档是一份计算机软考初级程序员的经典面试题汇编,涵盖了面向对象编程的四大特征(抽象、继承、封装、多态),并详细探讨了Java编程中的诸多核心概念,如基本数据类型与引用类型的区别、String和StringBuffer的差异、异常处理机制、Servlet的生命周期及其与CGI的区别、集合框架中ArrayList、Vector和LinkedList的特性对比、EJB的实现技术及其不同Bean类型的区别、Collection和Collections的差异、final、finally和finalize的作用、线程同步与异步的区别、抽象类和接口的区别、垃圾回收机制、JSP和Servlet的工作原理及其异同等。此外,还介绍了WebLogic服务器的相关配置、EJB的激活机制、J2EE平台的构成和服务、常见的设计模式(如工厂模式)、Web容器和EJB容器的功能、JNDI、JMS、JTA等J2EE核心技术的概念。 适合人群:正在备考计算机软考初级程序员的考生,或希望加深对Java编程及Web开发理解的初、中级开发人员。 使用场景及目标:①帮助考生系统复习Java编程语言的基础知识和高级特性;②为实际项目开发提供理论指导,提升编程技能;③为面试准备提供参考,帮助求职者更好地应对技术面试。 其他说明:文档不仅涉及Java编程语言的核心知识点,还包括了Web开发、企业级应用开发等方面的技术要点,旨在全面提高读者的专业素养和技术水平。文档内容详实,适合有一定编程基础的学习者深入学习和研究。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值