D - Smallest Difference
2017-11-30 18:30
155 查看
Smallest Difference
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
Sample Output
代码:
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
代码:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define INF 0x3f3f3f3f using namespace std; int main() { int t; char s[20]; int a[20]; cin>>t; getchar(); while(t--) { int i,j,k=0; gets(s); for(i=0;s[i]!='\0';i++) { if(s[i]>='0'&&s[i]<='9') a[k++]=s[i]-'0'; } if(k==2) { cout<<a[1]-a[0]<<endl; continue; } while(a[0]==0) //排除所有首元素为0的情况 next_permutation(a,a+k); //全排列 int ans=INF; do { int mid=k/2,m1=0,m2=0; if(a[mid]) { for(i=0;i<mid;i++) m1=m1*10+a[i]; for(i=mid;i<k;i++) m2=m2*10+a[i]; ans=min(ans,abs(m2-m1)); } }while(next_permutation(a,a+k)); cout<<ans<<endl; } return 0; }
相关文章推荐
- POJ 2718 - Smallest Difference(穷竭搜索)
- Smallest Difference
- Smallest Difference(POJ 2718)
- Smallest Difference:POJ - 2718(全排列)
- Smallest Difference (全排列 + 暴力?)
- 1239. Smallest Differencev
- soj 1239. Smallest Differencev
- 【枚举+贪心】POJ2718-Smallest Difference
- LintCode "The Smallest Difference"
- lintcode-medium-The Smallest Difference
- POJ2718-Smallest Difference-穷竭搜索
- POJ 2718 Smallest Difference 笔记
- [POJ](2718)Smallest Difference ---- 穷竭搜索
- POJ 2718 Smallest Difference
- Project Euler 44: Find the smallest pair of pentagonal numbers whose sum and difference is pentagonal.
- POJ 2718-Smallest Difference [dfs] 《挑战程序设计竞赛》2.1
- Smallest Difference-模拟
- POJ 2718 Smallest Difference (dfs)
- POJ 2718 Smallest Difference(暴力,全排列,next_permutation)
- #387 The Smallest Difference