Smallest Difference (dfs)深度搜索
来源:互联网 发布:rxjava gson解析数组 编辑:程序博客网 时间:2024/06/14 13:16
Smallest Difference
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 12001 Accepted: 3261
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.
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
10 1 2 4 6 7
Sample Output
28
Source
Rocky Mountain 2005
首先呢,这一串数字,要用字符串输入,在转换成数字存入数组;将数组排升序,
之后呢 我们c++的头文件algorithm里有一个全排列的函数,其实可以用dfs写出来数的全排列;
对于每一种全排列都取前n/2个数存入一个数s1中,后面的存入s2中(注意开头有零的可以跳过)
最后 求s1-s2绝对值的最小值;
这题还有以下思路 比如说有 7个数 此时可以将最小的4个数来组成最小的四位数s1,拿最大的3个数组成最大的3位数s2;
求s1-s2即可
比如说8个数 第一次 s1选个最小的非零数,s2选次非零数,有0的话,第二次s2选0,之后s1要先选大的组成一个大的数(不会比s2大) s2选最小的数,直到它们的位数为4 位置 求s2-s1即可
这种思路大家可以自己揣摩揣摩写一写,我还是给的第一种思路的代码
#include <iostream>#include <cstdio>#include <algorithm>#include<string.h>using namespace std;int a[20];//存入数 int main(){ int t; cin>>t; getchar(); while(t--) { int k=0,i; char s[30]; gets(s);//接受字符串 int l=strlen(s); //将字符串变数字存入数组 for(i=0;i<l; i+=2) { a[k++]=s[i]-'0'; } //排序 sort(a,a+k); int ans,mi=10000000; //对全排列进行分割总会找到最小的那个 do { int s1=0,s2=0; if((a[0]==0)||(a[k/2]==0&&k>2)) continue; for(int i=0;i<k/2;i++) s1=s1*10+a[i]; for(int j=k/2;j<k;j++) s2=s2*10+a[j]; ans=abs(s1-s2); mi=min(ans,mi); } while(next_permutation(a,a+k)); cout<<mi<<endl; } return 0;}
阅读全文
0 0
- Smallest Difference (dfs)深度搜索
- 深度优先搜索(DFS):POJ2718--Smallest Difference(解三)
- POJ 2718 Smallest Difference 【DFS 穷竭搜索】
- Smallest Difference (穷竭搜索)
- POJ 2718 Smallest Difference (dfs)
- POJ 2718 Smallest Difference (dfs)
- POJ 2718Smallest Difference(DFS)
- POJ NO.2718 Smallest Difference(DFS)
- POJ 2718 - Smallest Difference(双DFS)
- POJ 2718:Smallest Difference(dfs)
- poj-2718-Smallest Difference(dfs)
- [dfs]poj2718 Smallest Difference
- poj 2718 Smallest Difference(暴力搜索+STL+DFS)
- POJ 2718 - Smallest Difference(穷竭搜索)
- POJ 2718 Smallest Difference (穷竭搜索)
- Smallest Difference (POJ 2718, 穷竭搜索)
- Poj 2718 Smallest Difference【dfs】
- POJ2718-Smallest Difference-穷竭搜索
- HDU 1754 I Hate It (线段树+单点更新+区间询问)
- HDU4509 湫湫系列故事——减肥记II【格式输入+存储设置+暴力+水题】
- PAT 1023. 组个最小数 (20)
- C#-dictionary用法
- 怎么把mirtoolbox的提取函数的返回值转换成数字
- Smallest Difference (dfs)深度搜索
- windows上redis集群搭建附github下载地址
- javascript基础—keycode
- 关于MLX90614红外测温模块SMBus 协议的学习笔记<一>(附MLX90614中文资料,英文资料)
- this,call,apply
- N
- 【快速幂公式】“盛大游戏杯”第15届上海大学程序设计联赛夏季赛暨上海高校金马五校赛-添加好友
- UVA 140 Bandwidth
- css(cascading stylesheet language)的总结