双指针(对撞指针),模拟竞赛生的代码风格!
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 510;
int a[N];
int main(void) {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
sort(a + 1, a + n + 1);
int ans = 0, l = 1, r = n;
while (l < r) {
int s = a[l] + a[r];
if (!s) ++ans, ++l, --r;
else if (s < 0) ++l;
else --r;
}
printf("%d\n", ans);
}