poj 2718 又超时了 跪了 到底咋优化呢????????????

#include <stdio.h>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <iostream>
#include <algorithm>

using namespace std;
int a[11];
int testnum=0;
int n = 0;
int z1 = 999999999;


void solve(){
int x = 0, y = 0, z = 0;
int middle = n/2;
for (int i = 0; i < n; i++){
if (i < middle){
x += a[i]*pow(10.0, middle - i - 1);
}
else y += a[i]*pow(10.0, n - i - 1);
}
z = abs(x - y);
if(z1>z)z1=z;
}

int main(){
char ch;
scanf("%d ",&testnum);
for (int i = 0; i < testnum; i++){
while ((ch = getchar()) != '\n'){
if (ch == ' ')continue;
a[n] = ch - '0';
n++;
}
do{
if(a[0] == 0||a[n/2]== 0)continue;
solve ();
}while (next_permutation (a, a + n));
cout << z1;
}
return 0;
}

***************************

posted @ 2015-03-20 21:11  do+better  阅读(195)  评论(0编辑  收藏  举报