Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mi6112ogit
Created September 18, 2017 16:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mi6112ogit/a2d834daff05f9c5c2e8181542d9b59c to your computer and use it in GitHub Desktop.
Save mi6112ogit/a2d834daff05f9c5c2e8181542d9b59c to your computer and use it in GitHub Desktop.
#include <iostream>
#include <algorithm>
#include <utility>
#include <map>
#include <queue>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <sstream>
using namespace std;
#define FOR(i, j, k) for(int i = j; i < k; ++i)
#define rep(i, j) FOR(i, 0, j)
#define INF (1 << 30)
#define fi first
#define se second
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> Pi;
typedef pair<P, P> PP;
const int MOD = 1e9 + 7;
const int dy[]={0, 0, 1, -1};
const int dx[]={1, -1, 0, 0};
template <class T> void chmin(T& a, const T& b) { a = min(a, b); }
template <class T> void chmax(T& a, const T& b) { a = max(a, b); }
int digit[10];
int solve(int cnt) {
int res = INF;
do{
if (cnt / 2 != 1 && digit[0] == 0) continue;
if (cnt - cnt / 2 != 1 && digit[cnt / 2] == 0) continue;
int a = 0, b = 0;
rep(i, cnt / 2) a = a * 10 + digit[i];
FOR(i, cnt / 2, cnt) b = b * 10 + digit[i];
chmin(res, abs(a - b));
}while(next_permutation(digit, digit + cnt));
return res;
}
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
string in;
getline(cin, in);
n = atoi(in.c_str());
rep(i, n) {
string s;
getline(cin, s);
stringstream ss(s);
int cnt = 0;
while(!ss.eof()) {
ss >> digit[cnt++];
}
cout << solve(cnt) << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment