Smallest Difference

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 19528   Accepted: 5329

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

题意:n个数字分成两部分,构成两个整数,求这两个整数的最小差

题解:1、直接用函数next_pertumation()全排列
2、用dfs实现next_pertumation()功能
#include<iostream>
#include<string.h>
#include<string>
#include<algorithm>
#include<queue>
using namespace std;
int t,cnt,ans;
int a[];
string s; int main()
{
cin>>t;
getchar();
while(t--)
{
cnt=,ans=;
getline(cin,s);
for(int i=;s[i];i++)
{
if(isdigit(s[i]))
a[cnt++]=s[i]-'';
}
if(cnt==)//特例判断 0 1
{
cout<<abs(a[]-a[])<<endl;
continue;
}
while(a[]==)//如果首位数为0,在排一次序之后就不是了
next_permutation(a,a+cnt);
do
{
if(a[cnt/]!=)//首位不能为0
{
int num1=,num2=;
for(int i=;i<cnt/;i++)
num1=num1*+a[i];
for(int i=cnt/;i<cnt;i++)
num2=num2*+a[i];
ans=min(ans,abs(num1-num2));
} }while(next_permutation(a,a+cnt));
cout<<ans<<endl;
}
return ;
}
#include<iostream>
#include<string.h>
#include<string>
#include<algorithm>
#include<queue>
using namespace std;
int t,cnt,ans;
int a[],b[],vis[];
string s; void dfs(int dep)
{
if(dep==cnt)
{
int num1=,num2=;
for(int i=;i<cnt/;i++)
num1=num1*+b[i];
for(int i=cnt/;i<cnt;i++)
num2=num2*+b[i];
ans=min(ans,abs(num1-num2));
return ;
} for(int i=;i<cnt;i++)
{
if(vis[i]==)
continue;
if(dep==&&a[i]==)
continue;
if(dep==cnt/&&a[i]==)
continue;
vis[i]=;
b[dep]=a[i];
dfs(dep+);
vis[i]=;
}
}
int main()
{
cin>>t;
getchar();
while(t--)
{
memset(vis,,sizeof(vis));
cnt=,ans=;
getline(cin,s);
for(int i=;s[i];i++)
{
if(isdigit(s[i]))
a[cnt++]=s[i]-'';
}
if(cnt==)
{
cout<<abs(a[]-a[])<<endl;
continue;
}
if(cnt==)//防止tle
{
cout<<<<endl;
continue;
}
dfs();
cout<<ans<<endl;
}
return ;
}

POJ 2718 Smallest Difference dfs枚举两个数差最小的更多相关文章

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

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

  2. POJ 2718 Smallest Difference(最小差)

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

  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 枚举

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

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

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

  6. POJ 2718 Smallest Difference【DFS】

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

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

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

  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(全排列)

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

随机推荐

  1. STM32F4/F7运算性能

    参考http://bbs.21ic.com/icview-1622796-1-1.html

  2. .hpp 文件

    .hpp 是 Header Plus Plus 的简写,是 C++程序头文件. 其实质就是将.cpp的实现代码混入.h头文件当中,定义与实现都包含在同一文件,则该类的调用者只需要include该hpp ...

  3. NSObject类的API介绍

    这篇文章围绕的对象就是NSObject.h文件,对声明文件中的属性.方法进行必要的“翻译”. 该文件大致由两部分组成:NSObject协议和NSObject类. (一)NSObject协议 - (BO ...

  4. Swift3.0-基本运算符

    一.简介 运算符是检查.改变.合并值的特殊符号或者短语.在本篇文章中只介绍基本运算符,Swift中包含的高级运算符(比如溢出运算符)不在其中.Swift中的运算符和OC中的运算法还是有比较大的区别的, ...

  5. webdriver-js操作滚动条

    webdriver-js操作滚动条 1.      webdriver高级应用-js操作滚动条 1.滑动页面的滚动条到页面最下面 2.滑动页面的滚动条到页面的某个元素 3.滑动页面的滚动条向下移动某个 ...

  6. [转]No configuration found for the specified action 原因及解决方案

    转自 报错内容 警告: No configuration found for the specified action: 'login' in namespace: ''. Form action d ...

  7. python 基础之文件读操作

    创建一个名为‘尘曦’的文件内容如下 Hadoop是一个由Apache基金会所开发的分布式系统基础架构. 用户可以在不了解分布式底层细节的情况下,开发分布式程序.充分利用集群的威力进行高速运算和存储. ...

  8. HDU 5570:balls 期望。。。。。。。。。。。。。。。

    balls  Accepts: 19  Submissions: 55  Time Limit: 6000/3000 MS (Java/Others)  Memory Limit: 65536/655 ...

  9. Thymeleaf基本知识(推荐)

    原文: http://blog.csdn.net/pdw2009/article/details/44700897 Thymeleaf是个XML/XHTML/HTML5模板引擎,可以用于Web与非We ...

  10. YII insert multiple records into a table

    $values = array(array(1,2),array(3,4),array(5,6),); $nbValues = count($values); $sql = 'INSERT INTO ...