http://poj.org/problem?id=2718

题目大意:

给你一些数字(单个),不会重复出现且从小到大。他们可以组成两个各个位上的数字均不一样的数,如 0, 1, 2, 4, 6 ,7可以组成10 和 2467,但最小的差值由204和176组成,差值为28,这题就是求最小的差值。

思路:

直接枚举即可。

注意不能有前导0,当只有两个数时。。。比如0 和2答案为2,分开讨论就是了。

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
const int INF=0x3fffffff;
int a[12],len;
char c;
int solve(int start,int en)
{
int res=0;
for(int j=start;j<en;j++)
{
if(a[start]==0)
return INF;
res=res*10+a[j];
}
return res==0? INF:res;
}
int main()
{
int T;
scanf("%d",&T);
getchar();
while(T--)
{
len=0; while(scanf("%c",&c),c!='\n')
{
if(c==' ') continue;
a[len++]=c-'0';
}
int ans=INF;
if(len==2)
ans=a[1]-a[0];
else
do
{
int x=solve(0,len>>1);
int y=solve(len>>1,len); int t=abs(x-y);
if(t<ans && x!=INF && y!= INF)
ans=t; x=solve(0,(len>>1)+1);
y=solve((len>>1)+1,len); t=abs(x-y);
if(t<ans && x!=INF && y!= INF)
ans=t;
}while(next_permutation(a,a+len)); printf("%d\n",ans);
} return 0;
}

POJ 2718 Smallest Difference 枚举的更多相关文章

  1. POJ 2718 Smallest Difference(最小差)

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

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

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

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

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

  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(穷竭搜索dfs)

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

  6. 穷竭搜索: POJ 2718 Smallest Difference

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

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

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

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

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

  9. POJ 2718 Smallest Difference【DFS】

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

随机推荐

  1. qt qlineedit只输入数字

    lineEdit->setValidator(new QRegExpValidator(QRegExp("[0-9]+$")));

  2. cogs 1500. 误差曲线

    1500. 误差曲线 ★★   输入文件:errorcurves.in   输出文件:errorcurves.out   评测插件时间限制:1 s   内存限制:256 MB [题目描述] Josep ...

  3. hdu5024

    思路要开阔些,或者说要转化一下思路,别太死 把每一个点当拐点,爆一边就能够.用记忆化搜索也行.都不会超时 #include<bits/stdc++.h> using namespace s ...

  4. 游戏开发之UDK引擎介绍和模型导入

    2014-09-18 10:01:3 3.7.5" style="border:0px; vertical-align:middle; max-width:100%"&g ...

  5. js---16原型链

    var p = {name:"sss"}; var c2 = Object.create(p,{age:32,salar:"eee"});//c2就继承了p的属 ...

  6. TCP连接状态详解

    tcp状态: LISTEN:侦听来自远方的TCP端口的连接请求 SYN-SENT:再发送连接请求后等待匹配的连接请求 SYN-RECEIVED:再收到和发送一个连接请求后等待对方对连接请求的确认 ES ...

  7. 简单说一下 JSON和JSONP

    JSON和JSONP,但从缩写看,可能会以为他们是很相似的两个名词,但他们除了缩写相似外,他们是两种类型的概念. 首先: JSON(JavaScript Object Notation)即JavaSc ...

  8. 关于 js 的框架方向

    关于 js 的框架方向 http://www.breck-mckye.com/blog/2014/12/the-state-of-javascript-in-2015/?utm_source=ourj ...

  9. 运动识别之HOJ3D和HMM

    http://cvrc.ece.utexas.edu/Publications/Xia_HAU3D12.pdf   View Invariant Human Action Recognition Us ...

  10. Atcoder ABC 071 C,D

    C - Make a Rectangle Time limit : 2sec / Memory limit : 256MB Score : 300 points Problem Statement W ...