一、题目

Description

Given a number of distinct decimal digits, you can form one integer by choosing a non-empty subset of these digits and writing them in some order. The remaining digits can be written down in some order to form a second integer. Unless the resulting integer is 0, the integer may not start with the digit 0. 

For example, if you are given the digits 0, 1, 2, 4, 6 and 7, you can write the pair of integers 10 and 2467. Of course, there are many ways to form such pairs of integers: 210 and 764, 204 and 176, etc. The absolute value of the difference between the integers in the last pair is 28, and it turns out that no other pair formed by the rules above can achieve a smaller difference.

Input

The first line of input contains the number of cases to follow. For each case, there is one line of input containing at least two but no more than 10 decimal digits. (The decimal digits are 0, 1, ..., 9.) No digit appears more than once in one line of the input. The digits will appear in increasing order, separated by exactly one blank space.

Output

For each test case, write on a single line the smallest absolute difference of two integers that can be written from the given digits as described by the rules above.

Sample Input

1
0 1 2 4 6 7

Sample Output

28

二、思路&心得

  • next_permutation()函数的用法:注意若要得到全排列,则数组应该为有序的
  • 利用dfs + 回溯,再借以辅助数据visit可以找出一组数据的多种组合

三、代码


#include<cstdio>
#include<algorithm>
#define MAX 99999
using namespace std; int nums[11];
int t, len;
char ch; void solve() {
while (t --) {
len = 0;
while (1) {
scanf("%d%c", &nums[len ++], &ch);
if (ch == '\n') break;
}
if (len == 2) {
printf("%d\n", abs(nums[0] - nums[1]));
continue;
}
int num1, num2, ans = MAX;
int mid = len / 2;
do {
num1 = nums[0], num2 = nums[mid];
if (!num1 || !num2) continue;
for (int i = 1; i < mid; i ++) {
num1 = num1 * 10 + nums[i];
}
for (int i = mid + 1; i < len; i ++) {
num2 = num2 * 10 + nums[i];
}
if (abs(num1 - num2) < ans) ans = abs(num1 - num2);
} while (next_permutation(nums, nums + len));
printf("%d\n", ans);
}
} int main() {
scanf("%d", &t);
solve();
return 0;
}

【搜索】POJ-2718 全排列+暴力的更多相关文章

  1. POJ 2718【permutation】

    POJ 2718 问题描述: 给一串数,求划分后一个子集以某种排列构成一个数,余下数以某种排列构成另一个数,求这两个数最小的差,注意0开头的处理. 超时问题:一开始是得到一个数列的组合之后再从中间进行 ...

  2. POJ 2718 Smallest Difference(最小差)

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

  3. poj 2718 Smallest Difference(暴力搜索+STL+DFS)

    Smallest Difference Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6493   Accepted: 17 ...

  4. 穷竭搜索: POJ 2718 Smallest Difference

    题目:http://poj.org/problem?id=2718 题意: 就是输入N组数据,一组数据为,类似 [1  4  5  6  8  9]这样在0~9之间升序输入的数据,然后从这些数据中切一 ...

  5. [暴力搜索] POJ 3087 Shuffle'm Up

    Shuffle'm Up Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10003   Accepted: 4631 Des ...

  6. poj 2718 Smallest Difference(穷竭搜索dfs)

    Description Given a number of distinct , the integer may not start with the digit . For example, , , ...

  7. poj 2718 切数问题 穷竭搜索

    题意: 给一个已经排序号的数字,从中间切一刀,成两个数,要求这两个数的差最小 思路:暴力比较差最小值 stl中的next_permutation()函数进行排列  注意:这个函数必须从小到大才可以排序 ...

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

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

  9. POJ - 2718 Smallest Difference(全排列)

    题意:将n个数字分成两组,两组分别组成一个数字,问两个数字的最小差值.要求,当组内数字个数多于1个时,组成的数字不允许有前导0.(2<=n<=10,每个数字范围是0~9) 分析: 1.枚举 ...

随机推荐

  1. Linux入门基础(一):Linux基本操作

    命令行BASH基本操作 Shell 用户不能直接操作内核,所以用户操作通过shell传递给内核 shell分为两种 : GUI 图形界面 (linux一般是GNOME) CLI 命令行界面 (linu ...

  2. C 修真之旅

    前言 - 那久远的故事 工作好多年, 有时脑海里总回想儿时看的梦. 那时还刚上初中, 班上个子小的同学, 闲暇娱乐可能就是看 <飘渺之旅> 之类的小说. 前几年尝试满足自己少年时的记忆. ...

  3. 大数据入门第二天——基础部分之zookeeper(上)

    一.概述 1.是什么? 根据凡技术必登其官网的原则,我们先去官网瞅一瞅:http://zookeeper.apache.org/ Apache ZooKeeper is an effort to de ...

  4. 数据增强利器--Augmentor

    最近遇到数据样本数目不足的问题,自己写的增强工具生成数目还是不够,终于在网上找到一个数据增强工具包,足够高级,足够傻瓜.想要多少就有多少!再也不怕数据不够了! 简介 Augmentor是一个Pytho ...

  5. 指定路由器(DR/BDR)和非指定路由器(DROTHER)的区别

    一:名词解释 DR:designated router 指定路由器. BDR:backup designated router 备份指定路由器. 二:DR/BDR出现的缘由 在广播网和NBMA网络中, ...

  6. Flutter - 创建侧滑菜单

    侧滑菜单在安卓App里面非常常见,比如Gmail,Google Play,Twitter等.看下图 网上也有很多创建侧滑菜单的教程,我也来记录一下,自己学习创建Drawer的过程. 1. 创建一个空的 ...

  7. UWP 五星评价(不跳转到龟速商店)

    之前写过一篇文章  UWP 五星好评  代码如下 var pfn = Package.Current.Id.FamilyName; await Launcher.LaunchUriAsync(new ...

  8. Android Service(上)

    一 Service简介 Service是Context的子类 Service是四大组件之一 用来在后台处理一些比较耗时的操作或者去执行某些需要长期运行的任务 二 注意 Service里面不能直接执行耗 ...

  9. ios手机处理keyup事件时的兼容性问题

    在安卓手机中没有任何问题,但在ios手机中出现当输入法中输入内容时,事件keyup没有效果 解决办法: //keyup的兼容性处理 var bind_name = 'input'; if (navig ...

  10. 006 --MySQL索引原理

    一 .索引的概念? 一般的应用系统,读写比例在10:1左右,而且插入操作和一般的更新操作很少出现性能问题,在生产环境中,我们遇到最多的,也是最容易出问题的,还是一些复杂的查询操作,因此对查询语句的优化 ...