Time Limit: 1000MS | | Memory Limit: 65536K |
Total Submissions: 10453 | | Accepted: 2855 |
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.
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
当n为奇数的时候,很明显,让最常的从高位到最低位递增,最短的从高位到低位递减。
当n为偶数的时候,要使得两个数的最高位的差最小,同时后面的每一位的差值最大,我们可以找到差值最小的两个数,并且使得这两个数尽量在中间,这样才会使得后面的差值最大。
我看到好多全排列的题,找了两个对拍发现了对方代码都有bug,这道题的测试数据有点水。
#include