- 最小差距
- 2015-10-11 22:33:23 @
为什么我的程序会Runtime Error?
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#define MAX 1000000
int t, tt, n, x, s, i, j, s1, s2, min, p;
int f[100];
int a[100];
bool used[100];
int main()
{
scanf("%d", &t);
for (tt = 0;tt < t;tt++)
{
memset(f, 0, sizeof(f));
scanf("%d", &n);
for (i = 0;i < n;i++)
{
int x;
scanf("%d", &x);
f[x]++;
}
s = 0;
for (i = 0;i < 10;i++)
{
f[i] %= 2;
if (f[i])
{
s++;
a[s] = i;
}
}
if (s % 2)
{
s1 = a[1];
s2 = 0;
for (i = 2, j = s;i != j;i++, j--)
{
s1 = s1 * 10 + a[i];
s2 = s2 * 10 + a[j];
}
printf("%d", s1 - s2);
}
else
{
min = MAX;
for (i = 2;i <= s;i++)
{
s1 = a[i];
s2 = a[i - 1];
memset(used, 0, sizeof(used));
used[i] = used[i - 1] = true;
p = 1;
for (j = 1;p < s / 2;j++)
if (used[j])
continue;
else
{
s1 = s1 * 10 + a[j];
p++;
}
p = 1;
for (j = s;p < s / 2;j--)
if (used[j])
continue;
else
{
s2 = s2 * 10 + a[j];
p++;
}
if (s1 - s2 < min)
min = s1 - s2;
}
printf("%d", min);
}
printf("\n");
}
}