POJ 2718 Smallest Difference
Posted Cumulonimbus
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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.
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!=‘\n‘){ 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\n", ans); 58 } 59 }
以上是关于POJ 2718 Smallest Difference的主要内容,如果未能解决你的问题,请参考以下文章
穷竭搜索: POJ 2718 Smallest Difference
POJ - 2718Smallest Difference(搜索 )