【搜索】POJ-2718 全排列+暴力
一、题目
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 全排列+暴力的更多相关文章
- POJ 2718【permutation】
POJ 2718 问题描述: 给一串数,求划分后一个子集以某种排列构成一个数,余下数以某种排列构成另一个数,求这两个数最小的差,注意0开头的处理. 超时问题:一开始是得到一个数列的组合之后再从中间进行 ...
- POJ 2718 Smallest Difference(最小差)
Smallest Difference(最小差) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 Given a numb ...
- poj 2718 Smallest Difference(暴力搜索+STL+DFS)
Smallest Difference Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6493 Accepted: 17 ...
- 穷竭搜索: POJ 2718 Smallest Difference
题目:http://poj.org/problem?id=2718 题意: 就是输入N组数据,一组数据为,类似 [1 4 5 6 8 9]这样在0~9之间升序输入的数据,然后从这些数据中切一 ...
- [暴力搜索] POJ 3087 Shuffle'm Up
Shuffle'm Up Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10003 Accepted: 4631 Des ...
- poj 2718 Smallest Difference(穷竭搜索dfs)
Description Given a number of distinct , the integer may not start with the digit . For example, , , ...
- poj 2718 切数问题 穷竭搜索
题意: 给一个已经排序号的数字,从中间切一刀,成两个数,要求这两个数的差最小 思路:暴力比较差最小值 stl中的next_permutation()函数进行排列 注意:这个函数必须从小到大才可以排序 ...
- 【POJ - 2718】Smallest Difference(搜索 )
-->Smallest Difference 直接写中文了 Descriptions: 给定若干位十进制数,你可以通过选择一个非空子集并以某种顺序构建一个数.剩余元素可以用相同规则构建第二个数. ...
- POJ - 2718 Smallest Difference(全排列)
题意:将n个数字分成两组,两组分别组成一个数字,问两个数字的最小差值.要求,当组内数字个数多于1个时,组成的数字不允许有前导0.(2<=n<=10,每个数字范围是0~9) 分析: 1.枚举 ...
随机推荐
- Mac开发中遇到的一些小问题解析
通过mac开发的过程中,有一些小问题出现,列如下,后续会持续增加: 1. 命令行清空废纸篓(jar包太多,倾倒废纸篓太慢) sudo rm -rfv ~/.Trash /Volumes/*/.Tras ...
- SAP交货单增强MV50AFZ1问题
在MV50AFZ1这个出口的子程序FORM USEREXIT_SAVE_DOCUMENT_PREPARE.中进行了一些控制 当VL01N创建交货单点击保存的时候检查行项目的信息,如果有问题给出TYPE ...
- maven第一天——入门与基本概念
一.什么是maven? 1.概述 核心点:项目构建.依赖管理.[更新]:源码关联 (如何关联源码:在依赖的jar上右击 maven download source即可) Maven是一个项目管理和综合 ...
- mybatis第二天——动态SQL与关联查询
大纲摘要: 1.输入映射和输出映射 a) 输入参数映射 b) 返回值映射 2.动态sql a) If b) Where c) Foreach d) Sql片段 3.关联查询 a) 一对一关联 b) 一 ...
- HDU - 5877 Weak Pair (dfs+树状数组)
题目链接:Weak Pair 题意: 给出一颗有根树,如果有一对u,v,如果满足u是v的父节点且vec[u]×vec[v]<=k,则称这对结点是虚弱的,问这棵树中有几对虚弱的结点. 题解: 刚开 ...
- #2009. 「SCOI2015」小凸玩密室
神仙题啊.完全想不出 首先看方案.可以从任意一个点开始,在这个点要先走完子树,然后走到父亲,再走兄弟,再走父亲的父亲,父亲的兄弟..一直走到1,1的另外一个子树,结束. 完全不会鸭.jpg 设f[i] ...
- Java类的加载的一个小问题
前言 之前写了一篇文章专门介绍了一下类的加载和对象的创建流程,然后收到了一个博友的疑问,觉得蛮好的,在这里和大家分享下. 博文地址:[Java基础]Java类的加载和对象创建流程的分析 疑问 类在加载 ...
- 程序员 vs HR(皮这么一下很开心)
最近网络上一段 HR VS 程序员 的表情包火了 来来来 我们近距离感受下 来源|网络:http://t.cn/RuTKC8B 哈哈哈!大家可以留言说说你们面试时候的趣事 更多内容关注公众号:51re ...
- 关于SDK-manager中我们需要下载哪些?
废话少说,直接看图说话…… 图片取自博客文章——链接跳转:点击跳转
- 【转】将Centos的yum源更换为国内的阿里云源
摘要: 阿里云是最近新出的一个镜像源.得益于阿里云的高速发展,这么大的需求,肯定会推出自己的镜像源. 阿里云Linux安装镜像源地址:http://mirrors.aliyun.com/ CentOS ...