稻花香里说丰年,听取WA声一片
查看原帖
稻花香里说丰年,听取WA声一片
931366
quanquhengzhezou楼主2024/7/16 13:51

U450868

#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;

int main() {
    int T;
    cin >> T;
    
    while (T--) {
        int n, k;
        cin >> n >> k;
        
        vector<int> nums(n);
        for (int i = 0; i < n; ++i) {
            cin >> nums[i];
        }
        
        unordered_map<int, int> hash; // 哈希表,用于存储已遍历过的元素及其下标
        
        bool found = false;
        for (int i = 0; i < n; ++i) {
            int target = k - nums[i];
            if (hash.count(target) > 0) {
                found = true;
                break;
            }
            hash[nums[i]] = i;
        
        
        if (found) {
            cout << "yes" << endl;
        } else {
            cout << "no" << endl;
        }
    }}
    
    return 0;
}
2024/7/16 13:51
加载中...