题目链接:

https://vjudge.net/problem/POJ-2718

题目大意:

有一列数,对其任意分成两组,每组按一定顺序可以组成一个数。问得到的两个数的差最小是多少。

思路:

直接dfs构造就行,注意不能有前导0。而且有数据需要特判

只有一个数字的时候需要特判,还有只有两个数字的时候也需要特判,这是因为如果含有0和其他的数字,由于在构造中不允许前导0的出现,所以如果有这种情况的话更新不了minans,所以只有两个数字的时候也需要特判

 #include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<set>
#include<map>
#include<cmath>
#include<sstream>
using namespace std;
typedef pair<int, int> Pair;
typedef long long ll;
const int INF = 0x3f3f3f3f;
int T, n, m, minans;
const int maxn = 1e5 + ;
int dir[][] = {,,,,-,,,-};
int a[];
bool v[];
void dfs(int num, int d)
{
if(d == n / )//递归出口
{
int b[], tot = ;
for(int i = ; i < n; i++)
if(!v[i])b[tot++] = a[i];
sort(b, b + tot);
do
{
if(!b[])continue;
int x = ;
for(int i = ; i < tot; i++)x = x * + b[i];
minans = min(minans, abs(num - x));
}while(next_permutation(b, b + tot));
return;
}
for(int i = ; i < n; i++)
{
if(!v[i])
{
v[i] = ;
dfs(num * + a[i], d + );
v[i] = ;
}
}
}
int main()
{
cin >> T;
getchar();
while(T--)
{
string s;
n = ;
minans = INF;
getline(cin, s);
stringstream ss(s);
while(ss >> a[n++]);
n--;//此处需要自减一,因为n在ss流完的时候又加了一
if(n == )//特例特判
{
cout<<abs(a[])<<endl;
}
else if(n == )
{
cout<<(abs(a[] - a[]))<<endl;
}
else
{
for(int i = ; i < n; i++)
{
memset(v, , sizeof(v));
if(!a[i])continue;
v[i] = ;
dfs(a[i], );
}
cout<<minans<<endl;
}
}
return ;
}

POJ-2718 Smallest Difference---DFS的更多相关文章

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

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

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

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

  3. POJ 2718 Smallest Difference(最小差)

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

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

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

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

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

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

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

  7. POJ 2718 Smallest Difference【DFS】

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

  8. 穷竭搜索: POJ 2718 Smallest Difference

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

  9. POJ 2718 Smallest Difference 枚举

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

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

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

随机推荐

  1. Go语言基础之19--web编程基础

    一.web编程基础 1.1 web工作方式 1.2 HTTP协议详解 a.http 请求包体 GET /domains/example/ HTTP/1.1 //请求行: 请求方法 请求URI HTTP ...

  2. ZPL打印机命令解释

    个人备忘: 1.装驱动,装驱动要装对应的ZPL或者EPL版本,目前发现GK888T无需选择,直接装GK888T即可,其他机型未知. 2.标签设计,文本部分用SimSun-ExtB字体,变量内容部分用Z ...

  3. bean copy

    最初采用apache beanutils,性能很低.后来转为hutool,但不能复制指定格式的日期,所以采用性能很高的com.github.yangtu222.BeanUtils 它已经实现了 cop ...

  4. 汉诺塔问题hdu 2065——找规律

    这类题目就是纸上模拟,找规律. 问题描述:在一块铜板上有三根杆,目的是将最左边杆上的盘全部移到右边的杆上,条件是不允许直接从最左(右)边移到最右(左)边(每次移动一定是移到中间杆或从中间移出),也不允 ...

  5. element-ui表格合并span-method

    先看一下合并后的样式,表格第二行,二三四列合并 官网给我们提供了span-method的方法可以进行表格合并,有4个参数返回:row,column,rowIndex,columnIndex;row和c ...

  6. nginx —— 理解nginx_upstream_jvm_route模块解决tomcat多节点session不一致问题

    这种方式不需要修改web工程只需要对nginx下载nginx_upstream_jvm_route插件,修改tomcat和nginx配置,就能解决session问题.由于这种方式不会把session存 ...

  7. Head First 设计模式笔记(适配器)

    1.定义: 将一个类的接口转换成客户期望的另外一个接口.适配器让原来不兼容的类可以合作无间. 例子:插座转接头. 2.类图: 3.说明: 埋坑 4.例子 埋坑

  8. numpy初用

    import numpy as np for k,v in stat.iteritems():     print k     v.sort()     #v = v[len(v)*3/100:len ...

  9. Unity string 转换为 Quaternion

    public Quaternion QuaternionParse(string name) { name = name.Replace("(", "").Re ...

  10. pat1100. Mars Numbers (20)

    1100. Mars Numbers (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue People o ...