POJ2718 Smallest Difference
来源:互联网 发布:国内西装品牌 知乎 编辑:程序博客网 时间:2024/06/05 20:04
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
题目要求一定要看清楚,除非只有两个数不然不可以有前导0。
大概思路:全排列,分两半,求最小。
#include <iostream>#include<algorithm>#include<string>using namespace std;int a[15];int n;void solve();int main(){ int t; string str; cin>>t; getline(cin,str); while(t--){ n=0; getline(cin,str); for(int i=0;i<str.length();i++){ if(str[i]>='0'&&str[i]<='9') a[n++]=str[i]-'0'; } solve(); } return 0;}void solve(){ int m,ans,i; m=n/2;ans=100000; do{ if(n>2&&(!a[0]||!a[m])) continue; int d1,d2; d1=d2=0; for(i=0;i<m;i++){ d1=d1*10+a[i]; } for(i=m;i<n;i++){ d2=d2*10+a[i]; } ans=min(ans,abs(d1-d2)); }while(next_permutation(a,a+n)); cout<<ans<<endl;}
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(枚举排列)
- 洛谷 P1009 阶乘之和
- Android之eclipse错误 error opening trace file: No such file or directory (2)解决办法总结
- dp(记忆化搜索) + 概率论(全概率)
- 基于0.9版本的kafka测试问题
- 使用CSS3实现60FPS动画
- POJ2718 Smallest Difference
- javascript面向对象学习笔记
- 没有主键的表如何查询表内数据重复条数?
- Spring-AOP-学习笔记
- 一篇文章看懂正则表达式
- Intent--显示意图和隐式意图
- Android应用程序获取ROOT权限的方法
- CM12.1在Nexus Player上面的安装与问题解决
- android之缓存