POJ - 2718 Smallest Difference(全排列)
题意:将n个数字分成两组,两组分别组成一个数字,问两个数字的最小差值。要求,当组内数字个数多于1个时,组成的数字不允许有前导0。(2<=n<=10,每个数字范围是0~9)
分析:
1、枚举n个数字的全排列。
2、当两组数字个数相同或只差1时组成的两个数字才可能出现最小差值。
3、0~cnt/2 - 1为前半组数字,cnt/2~cnt-1为后半组数字。
4、注意getchar()的位置。
#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 10 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int a[MAXN];
string s;
int cnt;
bool judge(){
if(a[0] == 0 && cnt / 2 - 0 > 1) return false;//前半组数字个数大于1
if(a[cnt / 2] == 0 && cnt - cnt / 2 > 1) return false;//后半组数字个数大于1
return true;
}
int main(){
int T;
scanf("%d", &T);
getchar();
while(T--){
getline(cin, s);
stringstream ss(s);
int x;
cnt = 0;
while(ss >> x){
a[cnt++] = x;
}
sort(a, a + cnt);
int ans = INT_INF;
do{
int sum1 = 0, sum2 = 0;
if(!judge()) continue;
for(int i = 0; i < cnt / 2; ++i){
sum1 = sum1 * 10 + a[i];
}
for(int i = cnt / 2; i < cnt; ++i){
sum2 = sum2 * 10 + a[i];
}
ans = Min(ans, abs(sum1 - sum2));
}while(next_permutation(a, a + cnt));
printf("%d\n", ans);
}
return 0;
}
POJ - 2718 Smallest Difference(全排列)的更多相关文章
- POJ 2718 Smallest Difference(最小差)
Smallest Difference(最小差) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 Given a numb ...
- POJ 2718 Smallest Difference dfs枚举两个数差最小
Smallest Difference Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19528 Accepted: 5 ...
- 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(暴力搜索+STL+DFS)
Smallest Difference Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6493 Accepted: 17 ...
- poj 2718 Smallest Difference(穷竭搜索dfs)
Description Given a number of distinct , the integer may not start with the digit . For example, , , ...
- 穷竭搜索: POJ 2718 Smallest Difference
题目:http://poj.org/problem?id=2718 题意: 就是输入N组数据,一组数据为,类似 [1 4 5 6 8 9]这样在0~9之间升序输入的数据,然后从这些数据中切一 ...
- POJ 2718 Smallest Difference 枚举
http://poj.org/problem?id=2718 题目大意: 给你一些数字(单个),不会重复出现且从小到大.他们可以组成两个各个位上的数字均不一样的数,如 0, 1, 2, 4, 6 ,7 ...
- POJ 2718 Smallest Difference【DFS】
题意: 就是说给你一些数,然后要求你使用这些数字组成2个数,然后求他们的差值最小. 思路: 我用的双重DFS做的,速度还比较快,其中有一个很重要的剪枝,若当前搜索的第二个数后面全部补零与第一个数所产生 ...
- POJ 2718 Smallest Difference(dfs,剪枝)
枚举两个排列以及有那些数字,用dfs比较灵活. dfs1是枚举长度短小的那个数字,dfs2会枚举到比较大的数字,然后我们希望低位数字的差尽量大, 后面最优全是0,如果全是0都没有当前ans小的话就剪掉 ...
随机推荐
- 查看 Secret【转】
可以通过 kubectl get secret 查看存在的 secret. 显示有两个数据条目,kubectl describe secret 查看条目的 Key: 如果还想查看 Value,可以用 ...
- linux7下nenux3.14的maven私服搭建和配置使用
为什么用私服,各种帖子很多,话不多说,直接奉上本博主的搭建过程. 一.环境准备:确保jdk和maven安装完成 二.下载nenux:地址:https://www.sonatype.com/downlo ...
- java.jvm调优
_amazing~ 基本: 整理:
- junit小试log4j及xml配置文件说明
上篇文章介绍了java日志框架使用情况,以及xml配置文件的简单说明.但主要还是根据别人的博客整理一下知识结构,只能是纸上谈兵,本文通过junit测试框架来玩玩log4j. 1.junit+l ...
- SQL添加列、非空、默认值
use MarcoBarcode go alter table [dbo].[WorkOrderRepairSheet] ADD needRepair int go ALTER TABLE [dbo] ...
- 不能安装64位office提示已安装32位如何处理
不能安装64位office提示已安装32位如何处理? 1 2 3 4 5 6 当你遇到提示说[无法安装64位版本的office,以为在您的PC上找一下32位程序......]时如果你还想安装64位 ...
- Linus Torvalds正式宣布Linux Kernel 5.1RC2 发布,相当正常
导读 Linus Torvalds刚刚发布了Linux Kernel 5.2-rc2,这是继上周关闭合并窗口和随后的RC1之后的第一个内核测试版本. 在本周合并后的窗口活动中,Linus评论道,“嘿, ...
- 51nod 1055:最长等差数列
1055 最长等差数列 基准时间限制:2 秒 空间限制:262144 KB 分值: 80 难度:5级算法题 收藏 取消关注 N个不同的正整数,找出由这些数组成的最长的等差数列. 例如:1 3 5 ...
- nodejs - fs模块 - 文件操作
1, fs.stat 检测是文件还是目录 2, fs.mkdir 创建目录 var fs = require('fs') fs.mkdir('./dir',function(err){ if(err ...
- Flask—路由的注册方法
第一种注册方法 from flask import Flask app = Flask(__name__) @app.route("/hello") # 第一种注册方法 def h ...