【题目大意】

按升序输出几个不同的数字,任意组成两个数字,输出最小的差值。

【思路】

虽然是在穷竭搜索的章节里找到的题目,但是我觉得不需要穷竭搜索,枚举一下就可以了,0MS。分为一下三种情况:

(1)如果只有两个数字,且其中第一个数字为0,则第二个数字就是答案。

(2)如果有奇数个数字,分为长度为l+1和l的两部分,则较大数为从小到大的l+1个数字,较小数为从大到小的l个数字。如果首位是0,就和第二个数字预先交换位置。

(3)如果有偶数个数字,则两个数字长度必定一致。枚举两个数的首位数字,较大数在余下数字汇总由小到大取,较小数由大到小取即可。

 #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
const int MAXN=+;
using namespace std;
char s[MAXN];
int digit[MAXN];
int leng; int per()
{
int num1,num2;
int ans=0x7fffffff;
for (int i=;i<leng;i++)
for (int j=i+;j<=leng;j++)
{
if (digit[i]== || digit[j]==) continue;
num1=digit[j],num2=digit[i]; int t=,p=-;
while (t<leng/)
{
p++;
if (p==i || p==j) continue;
t++;
num1=num1*+digit[p];
} t=,p=leng+;
while (t<leng/)
{
p--;
if (p==j || p==i) continue;
t++;
num2=num2*+digit[p];
}
if (num1-num2<ans) ans=num1-num2;
}
return (ans);
} int odd()
{
int num1=,num2=;
if (digit[]==) swap(digit[],digit[]);
for (int i=;i<=leng/;i++) num1=num1*+digit[i];
for (int i=leng;i>leng/;i--) num2=num2*+digit[i];
return (num1-num2);
} int main()
{
int kase;
scanf("%d",&kase);
getchar();
for (int cases=;cases<kase;cases++)
{
gets(s);
leng=-;
for (int i=;i<strlen(s);i+=)
{
leng++;
digit[leng]=s[i]-'';
} if (leng== && digit[]==) cout<<digit[]<<endl;
else
{
if ((leng+)%==) cout<<odd()<<endl;
else cout<<per()<<endl;
}
} return ;
}

【枚举+贪心】POJ2718-Smallest Difference的更多相关文章

  1. poj2718 Smallest Difference(dfs+特判,还可以贪心更快)

    https://vjudge.net/problem/POJ-2718 其实不太理解为什么10超时了.. 这题似乎是有贪心优化的方法的,我下面直接暴力了.. 暴力之余要特判两个点:1.超时点就是n=1 ...

  2. POJ-2718 Smallest Difference

    http://poj.org/problem?id=2718 从一些数里面选择一个子集组成一个数,余下的数组成另外一个数,(数不能以0开头)问两个数的差的绝对值最小是多少! 不管是奇数还是偶数,要想绝 ...

  3. (DFS、全排列)POJ-2718 Smallest Difference

    题目地址 简要题意: 给若干组数字,每组数据是递增的在0--9之间的数,且每组数的个数不确定.对于每组数,输出由这些数组成的两个数的差的绝对值最小是多少(每个数出现且只出现一次). 思路分析: 对于n ...

  4. poj2718 Smallest Difference

    思路: 暴力乱搞. 实现: #include <iostream> #include <cstdio> #include <sstream> #include &l ...

  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枚举两个数差最小

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

  7. POJ 2718 Smallest Difference(最小差)

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

  8. 【POJ - 2718】Smallest Difference(搜索 )

    -->Smallest Difference 直接写中文了 Descriptions: 给定若干位十进制数,你可以通过选择一个非空子集并以某种顺序构建一个数.剩余元素可以用相同规则构建第二个数. ...

  9. LintCode "The Smallest Difference"

    Binary search. class Solution { int _findClosest(vector<int> &A, int v) { , e = A.size() - ...

随机推荐

  1. js_判断当前页面是否有网络和网络连接超时

    2018-04-12 方法一:通过navigator.onLine属性判断,返回true为有联网状态,false为断网状态. //方法一 if(navigator.onLine) { console. ...

  2. js_在原有的日期上添加天数输出添加后的日期

    开始编码工作也有段时间了,想想没有留下点什么,有点遗憾.学到的一些经验,写写,分享一下.也给自己整理一下. 今天分享一下,在原有的日期上添加天数输出添加后的日期.开始做的时候,简单的思路是,直接用ne ...

  3. Python 用ctypes观察Python对象的内存结构 -- (转)

    !!!强烈推荐的好文章!!! 对象的两个基本属性 Python所有对象结构体中的头两个字段都是相同的: refcnt:对象的引用次数,若引用次数为0则表示此对象可以被垃圾回收了. typeid:指向描 ...

  4. hdu 1159 Common Subsequence(最长公共子序列 DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Jav ...

  5. hdu 1233 还是畅通工程 (最小生成树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1233 还是畅通工程 Time Limit: 4000/2000 MS (Java/Others)    ...

  6. SQL Workbench/J

    最近测试segment, 使用了一个新的DB--SQL Workbench/J, 参考文档:http://docs.aws.amazon.com/redshift/latest/mgmt/connec ...

  7. Linux二进制代码的阅读

    大多数时候,我们研究的是如何阅读源代码.但在一些情况下,比如源代码不公开 或得到源代码的代价很高的情况下,我们又不得不需要了解程序的行为,这 时阅读二进制文件就非常重要.假设现在有一个二进制可执行文件 ...

  8. [NOIP2011]刷水

    前几天做了NOIP2011的题,感觉不是那么难. 这边先做了两天的前两题,T3还没打. D1T1:顺次读入,分别判断是否覆盖即可,照例大水: #include<cstdio> ],b[], ...

  9. Shell脚本 - nginx启动脚本

    OS:CentOS/Redhat 系列 并在 Centos 6.7 和 Centos 7.2 上测试正常 #!/bin/bash # # auth: daxin # time: 2018/07/10 ...

  10. Python脚本 - 查询磁盘的读写次数信息

    测试系统为:Centos 6.7 Python版本为: 3.6.4 脚本功能:查看指定磁盘的读写及时间等相关信息 #!/usr/bin/env python3 from collections imp ...