题意:将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(全排列)的更多相关文章

  1. POJ 2718 Smallest Difference(最小差)

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

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

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

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

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

  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 枚举

    http://poj.org/problem?id=2718 题目大意: 给你一些数字(单个),不会重复出现且从小到大.他们可以组成两个各个位上的数字均不一样的数,如 0, 1, 2, 4, 6 ,7 ...

  8. POJ 2718 Smallest Difference【DFS】

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

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

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

随机推荐

  1. BSGS&ExBSGS

    BSGS&ExBSGS 求解形如 \[a^x\equiv b\pmod p\] 的高次同余方程 BSGS 假装\(gcd(a,p)=1\). 设\(m=\lceil\sqrt p \rceil ...

  2. css调试与样式优先级

    如何查看一个标签的当前css样式 如上图所示 先用标签选择器选择某个标签 然后在elements区域就会自动找到该标签 然后在右侧的styles区域整个区域都是该标签的样式,从上到下是显示的优先级,被 ...

  3. windows 禁用中文输入法(转)

    源博客地址:http://blog.csdn.net/xie1xiao1jun/article/details/17913967 windows 程序禁用中文输入法方法:1.添加windows头文件及 ...

  4. poker2的配置使用

    1.映射Win键的诸多功能 切换输入法,shift+alt本身已经支持了,所以Win+Space就不管了 Win+Space映射为Fn+Space Win+E映射为Fn+E Win+R映射为Fn+R ...

  5. 096-PHP循环使用next取数组元素

    <?php function return_item($arr,$num=0){ //定义函数 for($i=0;$i<$num;$i++){ //循环向前移动数组指针 next($arr ...

  6. 课程作业02-1-课后作业1-(1)使用组合数公式利用n!来计算

    1.设计思想:运用递归阶乘的函数,依次求出n!.k!.(n-k)!,再根据组合数的公式计算(n!/(k!*(n-k)!)). 2.程序流程图: 3.源程序代码: //信1605-3 20163429 ...

  7. 吴裕雄--天生自然C++语言学习笔记:C++ 数字

    下面是一个 C++ 中定义各种类型数字的综合实例: #include <iostream> using namespace std; int main () { // 数字定义 short ...

  8. .net微软企业库的事务回滚

    事务是自定义的一个操作序列. 其中的操作要么全部执行要么全部不执行,是一个不可分割的工作单位.通过事务,SQL Server能将逻辑相关的一组操作绑定在一起,以便服务器保持数据的完整性. Databa ...

  9. 批量go get 代码

    批量 go get 代码 Go

  10. hibernate注解 笔记

    1.hibernate使用@where实现条件过滤功能 其里面只有一个参数clause,完整用法是: @Where(clause = "VALID_FLAG=1") 可以加在实体类 ...