poj2718 Smallest Difference
生活随笔
收集整理的這篇文章主要介紹了
poj2718 Smallest Difference
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
思路:
暴力亂搞。
實現:
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 }?
轉載于:https://www.cnblogs.com/wangyiming/p/6581993.html
總結
以上是生活随笔為你收集整理的poj2718 Smallest Difference的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【MySQL】可重复读模式下 uniqu
- 下一篇: centos7.3挂在移动硬盘(亲测)