Smallest Difference POJ
来源:互联网 发布:零售软件排名 编辑:程序博客网 时间:2024/06/05 20:25
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.
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.
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.
10 1 2 4 6 7
28
#include<stdio.h>#include<string.h>#include<math.h>#include<algorithm>using namespace std;int main(){ int t,a[20]; char s[20]; scanf("%d",&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) { printf("%d\n",a[1]-a[0]); continue; } while(a[0]==0) next_permutation(a,a+k); int ans=999999999; 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)); printf("%d\n",ans); } return 0;}
阅读全文
0 0
- POJ 2718 Smallest Difference
- poj 2718 Smallest Difference
- POJ-2718-Smallest Difference
- poj 2718 Smallest Difference
- poj 2718 Smallest Difference
- POJ 2718-Smallest Difference
- POJ 2718 Smallest Difference
- POJ 2718 Smallest Difference
- POJ 2718 Smallest Difference
- POJ 2718 Smallest Difference
- Smallest Difference(POJ-2718)
- POJ 2718 Smallest Difference
- POJ-2718 Smallest Difference
- poj-2718 Smallest Difference
- poj 2718 Smallest Difference
- POJ--2718 Smallest Difference
- poj-2718-Smallest Difference
- Smallest Difference POJ
- 解决CentOS sudo提示用户不在sudoers文件中
- Unix时间戳(Unix timestamp)
- 物理磁盘的四种使用方式
- poj2031
- 小记studio Could not find property 'vectorDrawables' 问题
- Smallest Difference POJ
- MySQL
- Java
- 【机器学习】梯度下降法的证明
- raw_input( )和sys.stdin.readline( )的区别
- MySQL常用命令(备查)
- Python操作excel
- Chromium Base学习笔记 —— Weakptr
- 客户端设置WebService调用超时时间