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 }
  • 相关阅读:
    原来发布到主页是发布到博客园的主页啊,还真不知道
    sysobjects
    blend3破解文件,直接覆盖安装目录源文件即可。
    Microsoft Sql中char和varchar的区别与联系
    SQL脚本创建数据表,并给出主键,外键,唯一键,约束
    asp.net调试方法
    VS2010安装项目的系统必备中添加.NET 2.0
    c#,自定义安装,部署,创建自定义操作,自定义操作,安装验证,数据库,安装授权码,接收输入,判断
    Ext2.2系列(50篇)
    C# 获取24小时制的当前系统时间
  • 原文地址:https://www.cnblogs.com/cumulonimbus/p/5855248.html
Copyright © 2011-2022 走看看