思路:
暴力乱搞。
实现:
1 #include <iostream> 2 #include <cstdio> 3 #include <sstream> 4 #include <algorithm> 5 #include <vector> 6 #include <cmath> 7 using namespace std; 8 9 const int INF = 0x3f3f3f3f; 10 11 bool in[10]; 12 int t, n, num[10]; 13 string s; 14 stringstream ss; 15 int minn = INF; 16 17 int main() 18 { 19 cin >> t; 20 getchar(); 21 while (t--) 22 { 23 getline(cin, s); 24 ss.clear(); 25 n = 0; 26 minn = INF; 27 ss << s; 28 while (ss >> num[n++]); 29 n--; 30 if (n == 2) 31 { 32 cout << abs(num[0] - num[1]) << endl; 33 continue; 34 } 35 do 36 { 37 int x = 0, y = 0, z = 0; 38 for (int i = 0; i < n / 2; i++) 39 { 40 x += num[i]; 41 if (i != n / 2 - 1) 42 x *= 10; 43 } 44 for (int i = n / 2; i < n; i++) 45 { 46 y += num[i]; 47 if (i != n - 1) 48 y *= 10; 49 if (i == n / 2) 50 continue; 51 z += num[i]; 52 if (i != n - 1) 53 z *= 10; 54 } 55 if (!(n > 3 && num[0] == 0 || num[n / 2] == 0)) 56 minn = min(minn, abs(x - y)); 57 if (n & 1 && !(num[0] == 0 || n > 3 && num[n / 2 + 1] == 0)) 58 minn = min(minn, abs(x * 10 + num[n / 2] - z)); 59 } while (next_permutation(num, num + n)); 60 cout << minn << endl; 61 } 62 return 0; 63 }