你可能判错了首尾相等 a1=a2n 的情况。
错误:
for (int i = 2; i < 2 * n; i++) {
if (a[i] == a[1]) pos1 = i;
if (a[i] == a[2 * n]) pos2 = i;
}
正确:
for (int i = 2; i <= 2 * n; i++)
if (a[i] == a[1]) pos1 = i;
for (int i = 1; i < 2 * n; i++)
if (a[i] == a[2 * n]) pos2 = i;