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. [BPNN]BP神经网络概念

    BP神经网络概念 BP神经网络的计算过程: 由正向计算过程和反向计算过程组成: 正向计算过程,输入模式从输入层经隐单元层逐层处理,并转向输出层,每一层神经元的状态只影响下一层神经元的状态.如果在输出层 ...

  2. 到底是哪个“O”管理内部人员风险?

    导读 俗话说,家和万事兴.与之相对的,家不睦则必自败.同理,如果缺乏明确的领导,内部人员风险管理项目或内部人威胁项目 (ITP) 也将走向失败. 俗话说,家和万事兴.与之相对的,家不睦则必自败.同理, ...

  3. Python笔记4

    JSON: JavaScript Object Notation,  JavaScript 对象标记 JSON 本质:是一种轻量级的数据交换格式 1. 轻量级 是 和 XML作比较 2. 数据交换格式 ...

  4. 解Bug之路-记一次调用外网服务概率性失败问题的排查

    前言 和外部联调一直是令人困扰的问题,尤其是一些基础环境配置导致的问题.笔者在一次偶然情况下解决了一个调用外网服务概率性失败的问题.在此将排查过程发出来,希望读者遇到此问题的时候,能够知道如何入手. ...

  5. 将UIImage转换成圆形图片image

    建议写成UIImage分类,如下: .h //变成圆形图片 - (UIImage *)circleImage; .m //变成圆形图片 - (UIImage *)circleImage { // NO ...

  6. unity优化-内存(网上整理)

    内存优化内存的开销无外乎以下三大部分:1.资源内存占用:2.引擎模块自身内存占用:3.托管堆内存占用.在一个较为复杂的大中型项目中,资源的内存占用往往占据了总体内存的70%以上.因此,资源使用是否恰当 ...

  7. static的特性

    1,static方法就是没有this的方法.在static方法内部不能调用非静态方法,反过来是可以的.而且可以在没有创建任何对象的前提下,仅仅通过类本身来调用static方法.这实际上正是static ...

  8. day 11 笔记

    # 装饰器形成的过程 : 最简单的装饰器 有返回值的 有一个参数 万能参数 # 装饰器的作用 # 原则 :开放封闭原则 # 语法糖 :@ # 装饰器的固定模式 #不懂技术 import time # ...

  9. keil遇到hardfault时原因的查找

    当硬件仿真遇到hardfault会进入响应的中断软件陷阱中void HardFault_Handler(void),此时通过view-registers中的 1 如果STACK=MSP,则查看SP的堆 ...

  10. 记录一次Nginx使用第三方模块fair导致的线上故障排错

    一.问题 今天发现有一台服务器的内存飙升,然后有预警,立即排查,发现该服务内存使用达到了 2G ,询问开发,当天是否有活动,被告知没有,登陆 Pinpoint 发现该服务是有两台机器,并且所有的访问都 ...