POJ2718 Smallest Difference
来源:互联网 发布:java计算器布局 编辑:程序博客网 时间:2024/06/05 12:33
题意:
给出一些数,分成两块,问分成的这两个数之差最小是多少。用的next_permutation,从小打大排序。
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
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<cmath>#define inf 1<<30using namespace std;typedef long long ll;char s[30];int main(){ int t; int num[15]; cin>>t; getchar(); while(t--) { int k=0; gets(s); int l=strlen(s); for(int i=0;i<l;i++) { if(s[i]>='0'&&s[i]<='9') num[k++]=s[i]-'0'; } if(k==2) { cout<<num[1]-num[0]<<endl; continue; } while(num[0]==0) next_permutation(num,num+k); int ans=inf; do{ int a,b; int mid=(k+1)/2; if(num[mid]) { a=0,b=0; for(int i=0;i<mid;i++) a=a*10+num[i]; for(int i=mid;i<k;i++) b=b*10+num[i]; ans=min(ans,abs(b-a)); } }while(next_permutation(num, num+k)); cout<<ans<<endl; } return 0;}
阅读全文
0 0
- poj2718 Smallest Difference
- poj2718 Smallest Difference
- POJ2718---Smallest Difference
- [dfs]poj2718 Smallest Difference
- POJ2718-Smallest Difference
- Smallest Difference poj2718
- POJ2718 Smallest Difference
- POJ2718-Smallest Difference
- poj2718 Smallest Difference【贪心】
- POJ2718 Smallest Difference
- poj2718 Smallest Difference
- POJ2718 Smallest Difference 【贪心+枚举】
- POJ2718——Smallest Difference
- POJ2718-Smallest Difference-穷竭搜索
- POJ2718 Smallest Difference (暴力搜索+全排列)
- Smallest Difference(Poj2718)(枚举全排列next_Permutation)
- POJ2718——Smallest Difference(暴力瞎搞)
- 【POJ2718】Smallest Difference(枚举排列)
- BootStrap入门
- YV12,I420,YUV420P的区别
- Mac 操作积累笔记
- spring初学
- Redis 学习笔记4: Redis 3.2.1 集群搭建
- POJ2718 Smallest Difference
- javaWeb:servlet
- 航线--dijkstra
- ImportError: No module named PIL
- 光驱作为yum源并开机自动挂载光驱
- 关于android学习开篇 颜色
- 从零开始 React 服务器渲染
- Hive的聚合函数
- java从小白到架构师大牛必看书籍