zoukankan      html  css  js  c++  java
  • POJ 2718 Smallest Difference

    Smallest Difference
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 8463   Accepted: 2324

    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
    

    Source

     
        注意代码第46行中 n>2 的条件。
     1 #include <iostream>
     2 #include <algorithm>
     3 #include <map>
     4 #include <vector>
     5 #include <functional>
     6 #include <string>
     7 #include <cstring>
     8 #include <queue>
     9 #include <set>
    10 #include <cmath>
    11 #include <cstdio>
    12 using namespace std;
    13 #define IOS ios_base::sync_with_stdio(false)
    14 #define TIE std::cin.tie(0)
    15 #define MIN2(a,b) (a<b?a:b)
    16 #define MIN3(a,b) (a<b?(a<c?a:c):(b<c?b:c))
    17 #define MAX2(a,b) (a>b?a:b)
    18 #define MAX3(a,b,c)  (a>b?(a>c?a:c):(b>c?b:c))
    19 typedef long long LL;
    20 typedef unsigned long long ULL;
    21 const int INF = 0x3f3f3f3f;
    22 const double PI = 4.0*atan(1.0);
    23 
    24 int ca, n, a[15], ans, m1, m2, x;
    25 int Input()
    26 {
    27     int n = 0;
    28     char ch=' ';
    29     while (ch!='
    '){
    30         if ('0' <= ch&&ch <= '9'){
    31             a[n++] = ch - '0';
    32         }
    33         ch = getchar();
    34     }
    35     return n;
    36 }
    37 int main()
    38 {
    39     scanf("%d", &ca);
    40     getchar();
    41     while (ca--){
    42         n = Input();
    43         sort(a, a + n);
    44         ans = INF;
    45         do{
    46             if (a[0] == 0 || a[n / 2] == 0 && n>2) continue;
    47             m1 = m2 = 0;
    48             for (int i = 0; i < n / 2; i++){
    49                 m1 *= 10;  m1 += a[i];
    50             }
    51             for (int i = n / 2; i < n; i++){
    52                 m2 *= 10;  m2 += a[i];
    53             }
    54             x = abs(m1 - m2);
    55             if (x < ans) ans = x;
    56         } while (next_permutation(a, a + n));
    57         printf("%d
    ", ans);
    58     }
    59 }
  • 相关阅读:
    HihoCoder 1638 : 小Hi的天平 (2-sat+并查集)
    阿里云安全肖力:云上数据安全体系建设的六要素
    MaxCompute客户端(odpscmd)在windows命令行下查询中文乱码问题处理实践
    序列化方案选型对比
    亚洲唯一,阿里云SLB位列Gartner全球网络负载均衡市场前五
    阿里云OSS同城冗余存储技术解析
    OSS跨同城3AZ重磅发布,构造全面数据保护体系
    阿里云OSS同城冗余存储正式商业化,提供云上同城容灾能力
    云原生应用 Kubernetes 监控与弹性实践
    GIAC2019 演讲精选 | 面向未来的黑科技——UI2CODE闲鱼基于图片生成跨端代码
  • 原文地址:https://www.cnblogs.com/cumulonimbus/p/5855248.html
Copyright © 2011-2022 走看看