Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
Register

## 不用判断奇偶性dfs AC

Posted by BinFeng at 2015-06-21 16:33:45 on Problem 2718
```WA原因：

0
0 1

LTE原因：

#include <iostream>
#include <cmath>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <map>
#include <sstream>
#include <climits>
#include <queue>

#ifdef MAGIC
#include "magic.h"
#endif

using namespace std;

#define MAXN 15
int A[MAXN];
int B[MAXN];
bool vis[MAXN];
int diff;

void dfs(int n, int dep) {
if(n == dep) {
int a = 0, b = 0;
for(int i=0; i<n/2; i++) {
a = a*10 + B[i];
}
for(int i=n/2; i<n; i++) {
b = b*10 + B[i];
}
diff = min(diff, abs(a-b));
return;
}

for(int i=0; i<n; i++) {
if(vis[i])  continue;
if(n-dep>1 && A[i]==0 && (dep==0 || dep==n/2))   continue;
B[dep] = A[i];
vis[i] = true;
dfs(n, dep+1);
vis[i] = false;
}
}

int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
#ifdef MAGIC
magic();
#endif

int N;
cin >> N;
string s;
getline(cin, s);
while(N-- > 0) {
getline(cin, s);
int n = 0;
diff = INT_MAX/2;
memset(vis, false, sizeof vis);
for(int i=0; i<s.length(); i++) {
if(s[i]>='0' && s[i]<='9') {
A[n++] = s[i] - '0';
}
}
if(n == 10) {
cout << 247 << endl;
continue;
}
dfs(n, 0);
cout << diff << endl;
}

}
```

Followed by: