poj 2718 Smallest Difference 【dfs(全排列变形题)】
来源:互联网 发布:mac网络连接鉴定失败 编辑:程序博客网 时间:2024/04/26 04:30
Smallest Difference
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 6389 Accepted: 1736
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
思路:
通过全排列就能将两个数的所有可能都排出来,然后取前一半数,来组成一个数,后一半数都保存到另一个数,然后做差就行了!不管是奇数个数还是偶数个数,都取n/2个数就行!
但是要特殊处理,如果是输入两个数(如:0 1),就会出错,所以要特殊处理,而如果输入10 个数,那么会造成超时(看讨论区说超时,但是试了试没超时,所以不用特判),所以将两个边界特殊考虑一下就OK了!
代码:
#include <stdio.h>#include <string.h>#include <algorithm>#define INF 0x3f3f3f3fusing namespace std;char a[25];int b[15];int used[15];int perm[15];int c[15];int num;int minn;int u,v;int vis[15];int perm2[15];void permutation(int pos,int n){if(pos==n){u=0,v=0;for(int i=0;i<n/2;i++){u=u*10+perm[i];}for(int i=n/2;i<n;i++){v=v*10+perm[i];}int dif=u-v;if(dif<0)dif=-dif;minn=minn>dif?dif:minn;return;}for(int i=0;i<num;i++){if(used[i])continue;if((pos==0||pos==num/2)&&b[i]==0)//pos<num-1&&(可以加上这一句,能够将0,1这个例子包含进去)continue;perm[pos]=b[i];used[i]=1;permutation(pos+1,n);used[i]=0;}}int main(){//freopen("shuju.txt","r",stdin);int T;scanf("%d",&T);getchar();//这个不能写到下面否则会出错! while(T--){gets(a);int len=strlen(a);int j=0;for(int i=0;i<len;i++){if(a[i]>='0'&&a[i]<='9'){b[j++]=a[i]-'0';//从小到大 }}num=j;if(num==2)//注意两个数字的时候,直接相减就行了!(如果不加这一句输入0,1就会出错!) {int s=b[0]-b[1];if(s<0)s=-s;printf("%d\n",s);continue;}if(num==10){printf("247\n");continue;}memset(used,0,sizeof(used));minn=INF;permutation(0,num); printf("%d\n",minn);}return 0;}
0 0
- poj 2718 Smallest Difference 【dfs(全排列变形题)】
- POJ 2718 Smallest Difference(DFS+全排列函数)
- POJ 2718 Smallest Difference(dfs 全排列)
- POJ 2718 Smallest Difference(暴力,全排列,next_permutation)
- Smallest Difference(poj 2718)二进制枚举+全排列
- poj 2718 Smallest Difference 穷竭搜索之全排列
- Poj 2718 Smallest Difference【dfs】
- POJ 2718 Smallest Difference (dfs)
- POJ-2718 smallest difference (DFS 随机数 剪枝)
- POJ 2718 Smallest Difference (dfs)
- POJ 2718Smallest Difference(DFS)
- POJ 2718 Smallest Difference 【DFS 穷竭搜索】
- poj 2718 Smallest Difference 【STL + dfs】
- POJ NO.2718 Smallest Difference(DFS)
- POJ 2718 - Smallest Difference(双DFS)
- POJ 2718:Smallest Difference(dfs)
- POJ 2718 Smallest Difference(双重DFS)
- poj-2718-Smallest Difference(dfs)
- 在eclipse上配置copy来的web项目
- hdoj Matrix multiplication 4920 (矩阵相乘)
- HDOJ 2531 Catch him 【 BFS 】
- 行为型模式之九——模板方法模式
- Java停止一个线程的几种方法
- poj 2718 Smallest Difference 【dfs(全排列变形题)】
- MAC $JAVA_HOME设置
- 自己新建activity和layout,发现setcontentview找不到id
- SQL防注入
- 【编程珠玑-读书笔记】算法设计技术—以求解"最大连续子序列和"为例
- ios中的数据存储
- WIN电脑优化
- java输入
- C语言版GetPrivateProfileString()函数,从初始化文件中指定的条目取得字串