#include <iostream>
#include <vector>
#include <unordered_map>

using namespace std;

int main() {
    int n, c;
    cin >> n >> c;
    vector<int> nums(n);
    for (int i = 0; i < n; i++) {
        cin >> nums[i];
    }
    
    unordered_map<int, int> count;
    int ans = 0;
    for (int i = 0; i < n; i++) {
        int a = nums[i];
        int b = a - c;
        if (count.find(b) != count.end()) {
            ans += count[b];
        }
        count[a]++;
    }
    
    cout << ans << endl;
    
    return 0;
}
给出一串数以及一个数字 c要求计算出所有 a-b=c 的数对的个数不同位置得到数字一样的数对算不同的数对。输入共两行。第一行两个整数nc 。第二行 a1a2a4 个整数 作为要处理的那串数。输出该数串中包含的满足a-b=c的数对的个数样例输入6 38 4 5 7 7 4样例输出5C++

原文地址: http://www.cveoy.top/t/topic/ja3u 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录