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;
}

poj 2718 Smallest Difference(暴力搜索+STL+DFS)的更多相关文章

  1. POJ 2718 Smallest Difference(最小差)

     Smallest Difference(最小差) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 Given a numb ...

  2. POJ 2718 Smallest Difference dfs枚举两个数差最小

    Smallest Difference Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19528   Accepted: 5 ...

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

    Smallest Difference Description Given a number of distinct decimal digits, you can form one integer ...

  4. poj 2718 Smallest Difference(穷竭搜索dfs)

    Description Given a number of distinct , the integer may not start with the digit . For example, , , ...

  5. 穷竭搜索: POJ 2718 Smallest Difference

    题目:http://poj.org/problem?id=2718 题意: 就是输入N组数据,一组数据为,类似 [1  4  5  6  8  9]这样在0~9之间升序输入的数据,然后从这些数据中切一 ...

  6. POJ 2718 Smallest Difference【DFS】

    题意: 就是说给你一些数,然后要求你使用这些数字组成2个数,然后求他们的差值最小. 思路: 我用的双重DFS做的,速度还比较快,其中有一个很重要的剪枝,若当前搜索的第二个数后面全部补零与第一个数所产生 ...

  7. POJ 2718 Smallest Difference(dfs,剪枝)

    枚举两个排列以及有那些数字,用dfs比较灵活. dfs1是枚举长度短小的那个数字,dfs2会枚举到比较大的数字,然后我们希望低位数字的差尽量大, 后面最优全是0,如果全是0都没有当前ans小的话就剪掉 ...

  8. POJ 2718 Smallest Difference 枚举

    http://poj.org/problem?id=2718 题目大意: 给你一些数字(单个),不会重复出现且从小到大.他们可以组成两个各个位上的数字均不一样的数,如 0, 1, 2, 4, 6 ,7 ...

  9. POJ - 2718 Smallest Difference(全排列)

    题意:将n个数字分成两组,两组分别组成一个数字,问两个数字的最小差值.要求,当组内数字个数多于1个时,组成的数字不允许有前导0.(2<=n<=10,每个数字范围是0~9) 分析: 1.枚举 ...

随机推荐

  1. (转)关于Android中为什么主线程不会因为Looper.loop()里的死循环卡死?引发的思考,事实可能不是一个 epoll 那么 简单。

    ( 转载请务必标明出处:http://www.cnblogs.com/linguanh/, 本文出自:[林冠宏(指尖下的幽灵)的博客]) 前序 本文将会把一下三个问题阐述清楚以及一个网上的普遍观点的补 ...

  2. 使用JavaScript和React编写原生移动应用

    使用JavaScript和React编写原生移动应用 React Native使你只使用JavaScript也能编写原生移动应用. 它在设计原理上和React一致,通过声明式的组件机制来搭建丰富多彩的 ...

  3. C C语言中 *.c和*.h文件的区别!

    一个简单的问题:.c和.h文件的区别学了几个月的C语言,反而觉得越来越不懂了.同样是子程序,可以定义在.c文件中,也可以定义在.h文件中,那这两个文件到底在用法上有什么区别呢? 2楼:子程序不要定义在 ...

  4. 数据库SQL优化分析查询语句总结

    方法一: SELECT TOP 10 TEXT AS 'SQL Statement' ,last_execution_time AS 'Last Execution Time' ,(total_log ...

  5. Java 实现 海康摄像头抓拍图像 Windows、Linux

    先抱怨一下,打死都想不到,海康的摄像头SDK居然是一个Java类,还有必须的两个jar包(jna.jar,examples.jar).鬼能想得到会这么命名. 下面开始吧. Windows 把从官网下载 ...

  6. 关于spring中bean配置的几件小事

    一.IOC和DI 1.IOC(Inversion of Control) 其思想是反转资源获取的方向.传统的资源查找方式要求组件向容器发起请求查找资源,作为回应,容器适时的返回资源:而应用了IOC之后 ...

  7. 剖析Vue之双向数据绑定

    vue.js 是采用数据劫持结合发布者-订阅者模式的方式,通过Object.defineProperty()来劫持各个属性的setter,getter,在数据变动时发布消息给订阅者,触发相应的监听回调 ...

  8. Linux--环境变量配置文件

    Linux系统中环境变量配置文件分为两类: 全局环境变量配置文件 /etc/profile 用户环境变量配置文件 ~/.bash_profile . ~/.bash_login ~/.profile ...

  9. 1 asp.net 中如何把用户控件应用于母版页

    1 创建用户控件 2 在母版页中注册用户控件 3 使用 <%@ Master Language="C#" AutoEventWireup="true" C ...

  10. Flutter 之页面状态保持

    一般情况下,我们使用tab切换的时候希望操作完毕之后,能够记住上个页面的状态, 但是使用Flutter的BottomNavigationBar的 时候默认是不记录页面状态的,即切换页面会导致重新加载. ...