【枚举+贪心】POJ2718-Smallest Difference
【题目大意】
按升序输出几个不同的数字,任意组成两个数字,输出最小的差值。
【思路】
虽然是在穷竭搜索的章节里找到的题目,但是我觉得不需要穷竭搜索,枚举一下就可以了,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的更多相关文章
- poj2718 Smallest Difference(dfs+特判,还可以贪心更快)
https://vjudge.net/problem/POJ-2718 其实不太理解为什么10超时了.. 这题似乎是有贪心优化的方法的,我下面直接暴力了.. 暴力之余要特判两个点:1.超时点就是n=1 ...
- POJ-2718 Smallest Difference
http://poj.org/problem?id=2718 从一些数里面选择一个子集组成一个数,余下的数组成另外一个数,(数不能以0开头)问两个数的差的绝对值最小是多少! 不管是奇数还是偶数,要想绝 ...
- (DFS、全排列)POJ-2718 Smallest Difference
题目地址 简要题意: 给若干组数字,每组数据是递增的在0--9之间的数,且每组数的个数不确定.对于每组数,输出由这些数组成的两个数的差的绝对值最小是多少(每个数出现且只出现一次). 思路分析: 对于n ...
- poj2718 Smallest Difference
思路: 暴力乱搞. 实现: #include <iostream> #include <cstdio> #include <sstream> #include &l ...
- POJ 2718 Smallest Difference(贪心 or next_permutation暴力枚举)
Smallest Difference Description Given a number of distinct decimal digits, you can form one integer ...
- POJ 2718 Smallest Difference dfs枚举两个数差最小
Smallest Difference Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19528 Accepted: 5 ...
- POJ 2718 Smallest Difference(最小差)
Smallest Difference(最小差) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 Given a numb ...
- 【POJ - 2718】Smallest Difference(搜索 )
-->Smallest Difference 直接写中文了 Descriptions: 给定若干位十进制数,你可以通过选择一个非空子集并以某种顺序构建一个数.剩余元素可以用相同规则构建第二个数. ...
- LintCode "The Smallest Difference"
Binary search. class Solution { int _findClosest(vector<int> &A, int v) { , e = A.size() - ...
随机推荐
- 项目记录 -- zpool set
zfs set <property=value> <filesystem|volume|snapshot> root@UA4300D-spa:~/hanhuakai/pro_0 ...
- Edgware Feign hystrix-dashboard
相关依赖 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring ...
- python中filter函数
python中filter()函数 filter()函数是 Python 内置的另一个有用的高阶函数,filter()函数接收一个函数 f 和一个list,这个函数 f 的作用是对每个元素进行判断 ...
- Java垃圾收集算法
算法名称 过程 优缺点 1. 标记-清除算法 (Mark-Sweep) 分为两个阶段: 1.首先标记出所有需要回收的对象: 2.在标记完成后统一回收所有被标记的对象. 缺点: 1.效率问题:标记和清除 ...
- Linux 入门记录:八、Linux 文件系统
一.文件系统 操作系统通过文件系统管理文件及数据,磁盘或分区需要创建文件系统之后,才能被操作系统所用,创建文件系统的过程又称之为格式化.没有文件系统的设备又称之为裸设备(raw),某些环境会需要裸设备 ...
- Cause: java.lang.ClassCastException: java.lang.String cannot be cast to org.apache.ibatis.mapping.MappedStatement
我用的是pagehelper 4.2.0,利用其进行表单的分页处理并进行展示,在第一次执行的时候能够看到分页后的结果,刷新一下第二次就显示不出来,控制台出现: Cause: java.lang.Cla ...
- [ python ] 类中的一些特殊方法
item系列 __getitem__(self, item) 对象通过 object[key] 触发 __setitem__(self, key, value) 对象通过 object[key] = ...
- [ python ] hasattr()、getattr()、setattr() 三者关系及运用
hasattr(object, name) 判断一个对象(object)是否存在name属性或方法,返回boolean值,有name属性返回True, 否则返回False In [1]: class ...
- 二、ansible配置简要介绍
[defaults] # some basic default values… hostfile = /etc/ansible/hosts \\指定默认hosts配置的位置 # library_pat ...
- Django web框架之权限管理二
1. login登录 def login(request): if request.method=="GET": return render(request,'login.html ...