首页 > 其他 > 详细

挑战程序设计竞赛2.1习题:Smallest Difference POJ - 2718

时间:2020-01-15 16:57:56      阅读:72      评论:0      收藏:0      [点我收藏+]

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

1
0 1 2 4 6 7

Sample Output

28

拿到题目,差分数字,列出所有的可能性,然后做差,取最小。但是,盲目暴力是肯定TLE的,所以考虑每种区分和排列,如果两组位数相差大,肯定没有两组位数一样的差小。所以,我们可以列除所有的排列,然后取前一半和后一半,就算是奇数相差一位也比差2位以上要小。自于全排列,我们用next_permutation()来玩,这玩意还挺智能的,迭代器也能使。
对整个数组全排列,在均分,前一半一个,后一半一个,这样可以使得所有情况都能遍历到(因为全排列本身在前半段后者后半段所有数字的组合都能出现,而且会进行排列)。
AC代码很短(借鉴了网上CSDN代码):
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <stdlib.h>
using namespace std;
int n1, n2;//分成两段算出的值
vector<int> a;//记录一开始的数组
string s;
int mindif;//最小差值
int main(void)
{
    int n;
    cin >> n;
    cin.get();
    for(int i = 0; i < n; i++)
    {    
        a.clear();
        mindif = 0x3fffffff;
        getline(cin, s);
        int sub = 0;
        for(int j = 0; j < s.size(); j++)
        {
            int t = 0;
            while(j < s.size() && s[j] !=  )
            {
                t *= 10;
                t += s[j] - 0;
                j++;
            }
            a.push_back(t);
        }
        if(a.size() == 2)//2个要特判,因为1/2==0
            mindif = abs(a[0] - a[1]);
    
else { do //do while,因为采用全排列函数的话就变成下一个升序的排列了 { n1 = n2 = 0; if(!a[0] || !a[a.size() / 2]) continue; for(int i = 0; i < a.size() / 2; i++) { n1 *= 10; n1 += a[i]; } for(int i = a.size() / 2; i < a.size(); i++) { n2 *= 10; n2 += a[i]; } mindif = min(mindif, abs(n1 - n2)); }while(next_permutation(a.begin(), a.end())); } printf("%d\n", mindif); } return 0; }

 

 

挑战程序设计竞赛2.1习题:Smallest Difference POJ - 2718

原文:https://www.cnblogs.com/jacobfun/p/12197309.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!