• poj 2718 Smallest Difference


                                                                                                  Smallest Difference
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 8418   Accepted: 2313

    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

    1
    0 1 2 4 6 7
    

    Sample Output

    28


    题意:输入若干数码,将这些数码任意划分成两部分并组合成两个整数,获得一个差值,求所有情况中差最小是多少
    思路:首先划分时两个数的位数越接近越好。
    思考若有N个数码,切割数码(或者说挑选数码)总共情况有2^N种,可以考虑成二进位制,**...*,共有N个星号,每个星号代表1或者0,分别表示每个数码取或者不取,这样对于每一种情况都能够将数码分成两块,对这两块数码都分别穷举各种排列方式求差的最小值。
    AC代码:
    #include<iostream>
    #include<string>
    #include<bitset>
    #include<algorithm>
    #include<math.h>
    using namespace std;
    int main() {
        int t;
        cin >> t;
        cin.ignore();
        while (t--) {
            string s;
            getline(cin, s);
            s.erase(remove(s.begin(),s.end(),' '),s.end());
            int length = s.size();
            int cut = length / 2;
            int permute = 1 << length;
            int result = INT_MAX;
            do {
                bitset<10>used = static_cast<bitset<10>>(permute);//将permute转化成有十位的二进制
                string s1, s2;
                for (int i = 0;i < length;i++) {
                    if (used[i])//每一个数码取或者不取
                        s1 += s[i];
                    else
                        s2 += s[i];
                }
                if (s1.size() != cut)
                    continue;
                if (s1[0] == '0'&&s1[0]>1)
                    continue;
             //切割完毕,穷举
                do {
                    int a = atoi(s1.c_str());
                    do {
                        if (s2[0] == '0'&&s2.size() > 1)
                            continue;
                        int b = atoi(s2.c_str());
                        int c = abs(a - b);
                        if (c < result)
                            result = c;
                    } while (next_permutation(s2.begin(), s2.end()));
                } while (next_permutation(s1.begin(), s1.end()));
            } while (permute--);
            cout << result << endl;
        }
        return 0;
    }
  • 相关阅读:
    Quartz定时调度CronTrigger时间配置格式说明与实例
    JAVA中堆和栈的区别
    s:iterator循环输出数字
    JAVA 遍历文件夹下的所有文件(递归调用和非递归调用)
    FilenameFilter总结
    Oracle sql"NOT IN"语句优化,查询A表有、B表没有的数据
    Java并发教程(Oracle官方资料)
    做考试系统用到的关于onbeforeunload一些兼容性问题
    Java集合类ArrayList循环中删除特定元素
    Log4j写入数据库详解
  • 原文地址:https://www.cnblogs.com/ZefengYao/p/5826465.html
Copyright © 2020-2023  润新知