← 返回题目页

P2141 [NOIP 2014 普及组] 珠心算测验

标程

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n;
    cin >> n;

    vector<int> a(n);
    for (int& x : a) cin >> x;

    set<int> s(a.begin(), a.end()); // 集合里的所有数
    set<int> hit;                  // 能写成两个不同的数之和的数

    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            int sum = a[i] + a[j]; // 两个加数取的是集合里不同的两个数
            if (s.count(sum)) hit.insert(sum);
        }
    }

    cout << hit.size() << "\n";
    return 0;
}

动画演示