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. HOJ——T 2430 Counting the algorithms

    http://acm.hit.edu.cn/hoj/problem/view?id=2430 Source : mostleg Time limit : 1 sec Memory limit : 64 ...

  2. hdu 5073 Galaxy(2014 鞍山现场赛)

    Galaxy                                                                   Time Limit: 2000/1000 MS (J ...

  3. 深度学习2015年文章整理(CVPR2015)

    国内外从事计算机视觉和图像处理相关领域的著名学者都以在三大顶级会议(ICCV.CVPR和ECCV)上发表论文为荣,其影响力远胜于一般SCI期刊论文.这三大顶级学术会议论文也引领着未来的研究趋势.CVP ...

  4. 与TCP/IP协议的初次见面(一)

    与TCP/IP协议的初次见面(一) 前言 今下午刚才女朋友那边回来,唉,算是情人节去找她吧,穷屌丝住不起好酒店,住的打折酒店,只是女朋友来姨妈了,万幸啊,牙还疼得不行,这几天光照应她了,没空写博客了. ...

  5. Lucene-全文索引

    近期接触了lucene,我想也有非常多人以前听过,于是带着好奇心,我開始对lucene进行了解,给我影响最深的是它非常多的应用了索引表,这个工具之所以快是就是由于大量引用到了索引表.今天仅仅说下我刚開 ...

  6. 搜寻Linux软件实用指南

    搜寻Linux软件实用指南  对于初学者来说,仅仅安装好Linux系统还是不够的,还需要安装大量的应用软件.许多下载网站都提供了诸如装机必备软件的下载,分门别类提供经典的工具软件下载.本文主要针对初学 ...

  7. iOS-APP-Icon 图标启动图及名字的设置

    本文讲下appIcon图标.启动图及名字的设置 icon for iOS 图标大小参照苹果官网:https://developer.apple.com/library/ios/qa/qa1686/_i ...

  8. string StartsWith 方法 Https

    public ActionResult Index()        {            string url = "Https://www.baodu.com";      ...

  9. 【2017 Multi-University Training Contest - Team 2】Maximum Sequence

    [Link]:http://acm.hdu.edu.cn/showproblem.php?pid=6047 [Description] 给你一个数列a和一个数列b; 只告诉你a的前n项各是什么; 然后 ...

  10. [Angular & Unit Testing] Automatic change detection

    When you testing Component rendering, you often needs to call: fixture.detectChanges(); For example: ...