原题链接
题解
代码
双指针
#include "iostream"
using namespace std;
const int N = 100010;
int a[N],b[N];
int main(){
int n,m,x;
cin>>n>>m>>x;
for(int i=0;i<n;i++)cin>>a[i];
for(int i=0;i<m;i++)cin>>b[i];
for(int i=0,j=m-1;i<n;i++){
while (j>=0&&a[i]+b[j]>x)j--;
if(a[i]+b[j]==x){
cout<<i<<' '<<j;
break;
}
}
}
二分查找
#include "iostream"
#include "algorithm"
using namespace std;
const int N = 100010;
int a[N],b[N];
int main(){
int n,m,x;
cin>>n>>m>>x;
for(int i=0;i<n;i++)cin>>a[i];
for(int i=0;i<m;i++)cin>>b[i];
for(int i=0;i<n;i++){
int target=x-a[i];
if(binary_search(b,b+m,target))cout<<i<<' '<<lower_bound(b,b+m,target)-b;
}
}
标签:main,const,100010,int,元素,namespace,数组,include,acwing
From: https://www.cnblogs.com/ChengMao/p/17135364.html