2 条题解

  • 1
    @ 2024-11-21 20:33:45
    #include<bits/stdc++.h>
    #define int long long
    using namespace std;
    const int INF = 0x3f3f3f3f;
    const int N = 1e5 + 10;
    int a [N] , n , m;
    signed main(){
    	    cin >> n;
    	    for (int i = 1; i <= n; i++)
    	    {
    	    	scanf ("%d" , &a[i]);
    		}
    		cin >> m;
    	    sort(a + 1 , a + 1 + n);
    	    int l = 1 , r = n;
    	    while(l <= r)
    		{
    		    if(a [l] + a [r] == m)
    			{
    			    cout << a [l] << " " << a [r];
    			    return 0;
    		    }
    		    else
    			{
    			    if(a[l] + a[r] < m)
    				{
    				   l++;
    			    }
    			    else
    				{
    				   r--;
    			    }
    		}
    	}
    	cout << "No";
    	return 0;
    }
    
    • -2
      @ 2024-12-17 16:27:04
      #include<bits/stdc++.h>
      using namespace std;
      long long a[1000005]={0};
      int main(){
      	int n,m;
      	cin>>n;
      	for(int i=1;i<=n;i++){
      		cin>>a[i];
      	}
      	cin>>m;//和为m
      	sort(a+1,a+n+1);
      	int l=1,r=n;//双指针 
      	while(l<=r){
      		if(a[l]+a[r]==m){
      			cout<<a[l]<<" "<<a[r];
      			return 0;
      		}
      		else if(a[l]+a[r]>m){
      			r--;
      		}
      		else{
      			l++; 
      		} 	
      	}
      	cout<<"No";
      	return 0;
      }
      小鸟
      
    • 1

    信息

    ID
    3112
    时间
    1000ms
    内存
    256MiB
    难度
    2
    标签
    递交数
    30
    已通过
    7
    上传者