zoukankan      html  css  js  c++  java
  • poj 2718 Smallest Difference(暴力)

    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

    题意:给出不超过十个1位数,问这些数排列组合出的两个前导不为零数差的绝对值最小是多少?
    思路:暴力枚举这些1位数的所有排列,用next_permutation函数是非常方便的。因为分成了两部分,所以从排列的中间进行组合再做差得到最后的结果。因为前导不是零,所以要判断当两个数前导为零时continue
    AC代码:
     1 #include <iostream>
     2 //#include <bits/stdc++.h>
     3 #include <cstdio>
     4 #include <cstring>
     5 #include <cmath>
     6 #include <algorithm>
     7 using namespace std;
     8 
     9 int main()
    10 {
    11     char s[3000];
    12     int a[20],n;
    13     while(~scanf("%d",&n))
    14     {
    15         getchar();
    16         while(n--)
    17         {
    18             gets(s);
    19             int len=strlen(s);
    20             int k=0;
    21             for(int i=0; i<len; i=i+2)
    22             {
    23                 if(s[i]>='0'&&s[i]<='9')
    24                 {
    25                     a[k++]=s[i]-'0';
    26                 }
    27             }
    28             int    _min=999999;
    29             int ans1=0,ans2=0;
    30             sort(a,a+k);
    31             do
    32             {
    33                 ans1=0,ans2=0;
    34                 if(!a[0]||!a[k/2]&&k>2)
    35                 continue;
    36                     for(int i=0; i<k/2; i++)
    37                         ans1=ans1*10+a[i];
    38                     for(int i=k/2; i<k; i++)
    39                         ans2=ans2*10+a[i];
    40                     if(abs(ans1-ans2)<_min)
    41                         _min=abs(ans1-ans2);
    42                 }while(next_permutation(a,a+k));
    43             printf("%d
    ",_min);
    44         }
    45     }
    46     return 0;
    47 }
    View Code
  • 相关阅读:
    应用程序池溢出问题
    弹窗上传图片
    第三方监测
    服务器架设方案
    python随笔录入月份的值,输出对应的季节
    用python计算直角三角形斜边长
    返回(统计)一个列表中出现次数最多的元素
    使用random函数实现randint函数的功能
    Spring
    ng build prod basehref /javaweb/angular/
  • 原文地址:https://www.cnblogs.com/wang-ya-wei/p/6212806.html
Copyright © 2011-2022 走看看