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

    Smallest Difference
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 6740   Accepted: 1837

    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

     
    给定一个数字序列,将其分为两部分,求差绝对值的最小值;
    next_permutation()即可,但需要判断首元素是否为0
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <cmath>
     6 using namespace std;
     7 int a[15];
     8 int main()
     9 {
    10     int T;
    11     scanf("%d",&T);
    12     getchar();
    13     while(T--)
    14     {
    15         int temp;
    16         int n=0,i,Min=10000000;
    17         while((temp=getchar())!='
    ')        
    18         {
    19             if(temp!=' ')
    20                 a[n++]=temp-'0';
    21         }
    22         sort(a,a+n);
    23 /*        for(i=0;i<n;i++)
    24             cout<<a[i]<<" ";
    25         cout<<endl;*/
    26         if(n==2&&a[0]==0)
    27         {
    28             cout<<a[1]<<endl;
    29             continue;
    30         }
    31         do{
    32             int num1=0,num2=0,m;
    33             if(a[0]==0||a[n/2]==0)
    34                 continue;
    35             for(i=0;i<n/2;i++)
    36                 num1=num1*10+a[i];
    37             for(;i<n;i++)
    38                 num2=num2*10+a[i];
    39             Min=min(Min,abs(num1-num2));
    40             //cout<<num1<<" "<<num2<<" "<<abs(num1-num2)<<" "<<Min<<endl;
    41         }while(next_permutation(a,a+n));
    42         printf("%d
    ",Min);
    43     }
    44 }
     
  • 相关阅读:
    oracle 自增序列实现 可作为主键
    oracle 10 升级补丁
    软件设计原则
    oracle查询截至到当前日期月份所在年份的所有月份
    Dockerfile 基本命令
    Mybatis自动生成代码,MyBatis Generator
    Java垃圾回收机制
    SqlServer 导入 .sql 文件
    备份个清库脚本
    备份一些开发配置
  • 原文地址:https://www.cnblogs.com/a1225234/p/5150799.html
Copyright © 2011-2022 走看看