标签:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8463 | Accepted: 2324 |
Description
Input
Output
Sample Input
1 0 1 2 4 6 7
Sample Output
28
Source
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 }
标签:
原文地址:http://www.cnblogs.com/cumulonimbus/p/5855248.html