poj 2718 Smallest Difference(暴力搜索+STL+DFS)

Smallest Difference
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6493 Accepted: 1771

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
 
 
 给你  《=10个数,从中可以任意取出几个数,余下的组成另一个数,试问这两个数的最小差是多少
有几个注意的地方:1.对于给定的序列,,要让其差最小,那么取出这的两个数位数则需要尽量向相同靠齐
               2.对于其中的一个序列的模拟,,是可以通过DFS将取出相同数字的所有不同放法遍历到的,所以
                 不需要双重DFS
               3。对于已经取出的数字,可以直接在循环时就求出其值,不需要再弄个单独的函数,对数组遍历进行求值 
              4.特别要注意的是,,在dfs中一定要用if  else if 语句,,否则会在 st==n/2+1之后
                也就是取出符合条件的数字之后,又一次进入循环内,,所以最后tle了好几次,,就少了个
                else ,,, 
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define inf 0x3f3f3f3f
using namespace std;
int a[12],b[12],s[12],minn,v[12],n;
int dfs(int st,int va) //3
{
     if(a[1]==0&&st>=2)
               return 0;
    if(st==n/2+1)
    {
        int r=0;
        for(int j=1;j<=n;j++)
            if(!v[j])
                  b[++r]=s[j];
          do{
                  if(r>=2&&b[1]==0)
                    continue;
                  int temp=0;
                  for(int i=1;i<=r;i++)
                    temp=temp*10+b[i];
                  if(abs(temp-va)<minn)
                        minn=abs(temp-va);
            }while(next_permutation(b+1,b+r+1));
    }
    else  //4!!!!!!!!!!!!!!!!!没有这个TL;E了好几次
    for(int i=1;i<=n;i++)
        if(!v[i])
        {
             v[i]=1;
             a[st]=s[i];
             dfs(st+1,va*10+s[i]);
             v[i]=0;
        }  //2
    return 0;
}
void init()
{
        memset(s,0,sizeof(s));
        memset(v,0,sizeof(v));
        n=0;
        char m[30];
        gets(m);
        for(int i=0;m[i]!='\0';i++)
            if(m[i]>='0'&&m[i]<='9')
                 s[++n]=m[i]-'0';
}
int main()
{
    int  cas;
    scanf("%d\n",&cas);
    while(cas--)
    {
        init();
        minn=inf;
        dfs(1,0);
        printf("%d\n",minn);
    }
    return 0;
}

转载于:https://www.cnblogs.com/smilesundream/p/6642555.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值