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

    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

    百度翻译:

    给定一组不同的十进制数字,您可以通过选择这些数字的非空子集并按一定顺序写入来形成一个整数。剩下的数字可以按某种顺序写下来形成第二个整数。除非得到的整数为0,否则整数不能以数字0开头。 例如,如果给定数字0、1、2、4、6和7,则可以编写整数对10和2467。当然,形成这样的整数对有很多种方法:210和764、204和176等,最后一对中的整数之差的绝对值是28,结果表明,由上述规则形成的其他对都不能达到较小的差。

    思路:由于输入的数据并不大,可以用全排列(next_permutation)的方式去写,将排列后的数分为前半部分和后半部分,
    求出两部分的差的最小值。即为答案。注意输入只有两个数的情况和去除排列时以0为开头的排列。

     1 #include <cstdio>
     2 #include <fstream>
     3 #include <algorithm>
     4 #include <cmath>
     5 #include <deque>
     6 #include <vector>
     7 #include <queue>
     8 #include <string>
     9 #include <cstring>
    10 #include <map>
    11 #include <stack>
    12 #include <set>
    13 #include <sstream>
    14 #include <iostream>
    15 #define mod 1000000007
    16 #define eps 1e-6
    17 #define ll long long
    18 #define INF 0x3f3f3f3f
    19 using namespace std;
    20 
    21 int t;
    22 string str;
    23 int sz[15];
    24 int main()
    25 {
    26     cin>>t;
    27     getchar();
    28     while(t--)
    29     {
    30         getline(cin,str);
    31         int ans=0;
    32         for(int i=0;i<str.size();i++)
    33         {
    34             if(str[i]>='0'&&str[i]<='9')
    35             {
    36                 sz[ans]=str[i]-'0';
    37                 ans++;
    38             }
    39         }
    40         if(ans==2)//只有两个数时
    41         {
    42             cout<<abs(sz[1]-sz[0])<<endl;
    43             continue;
    44         }
    45         while(sz[0]==0)//不能以0开头
    46         {
    47             next_permutation(sz,sz+ans);
    48         }
    49         int ma=999999999;
    50         do{
    51             int mid=(ans+1)/2;
    52             if(sz[mid])
    53             {
    54                 int a=0,b=0;
    55                 for(int i=0;i<mid;i++)
    56                 {
    57                     a=a*10+sz[i];
    58                 }
    59                 for(int i=mid;i<ans;i++)
    60                 {
    61                     b=b*10+sz[i];
    62                 }
    63                 ma=min(ma,abs(a-b));
    64             }
    65         }while(next_permutation(sz,sz+ans));
    66         cout<<ma<<endl;
    67     }
    68 }


  • 相关阅读:
    3年度研发项目情况项目目标文档
    2系统利益相关者描述案例
    讨论结果
    本学期《软件需求分析》需要掌握的内容(个人总结)
    第二周进度条
    四则运算
    第一周进度记录
    java web
    ke tang zuo ye
    2015-05-26 随笔
  • 原文地址:https://www.cnblogs.com/mzchuan/p/11196235.html
Copyright © 2011-2022 走看看