2 solutions

  • 0
    @ 2022-3-31 19:56:21

    萌新写法:枚举+二分

    #include <bits/stdc++.h>
    using namespace std;
    
    int n,m,size = 0,num[1000005];
    
    int check(int x){
        int left = -1,right = n,mid;
        while(left + 1 < right){
            mid = (left + right) / 2;
            if(num[mid] > x){
                right = mid;
            }
            else{
                left = mid;
            }
        }
        if(num[right - 1] == x){
            return 1;
        }
        return -1;
    }
    
    
    int main(){
        int list;
        cin>>n;
        for(int i = 0 ; i < n ; i++){
            cin>>num[i];
        }
        sort(num,num+n);
        cin>>m;
        for(int i = 0 ; i < n ; i++){
            list = check(m - num[i]);
            if(list != -1){
                size++;
            }
        }
        cout<<size/2<<endl;
        return 0;
    }
    

    Information

    ID
    1102
    Time
    1000ms
    Memory
    20MiB
    Difficulty
    6
    Tags
    # Submissions
    76
    Accepted
    21
    Uploaded By