刚学OI一秒,蒟蒻代码求调,感谢各位大佬
查看原帖
刚学OI一秒,蒟蒻代码求调,感谢各位大佬
1173402
yfhshtk楼主2024/1/14 17:33

萌新二分啥也不会,自己瞎写的手推样例可以过,但为啥啥也不输出?

#include<bits/stdc++.h>
#define int long long 
using namespace std;
const int MAXN=1e5+5;
int n,m,a[MAXN],b[MAXN];
bool check(int k,int l,int r){
	while(l<=r){
		int mid=(l+r)/2;
		if(k==b[mid]){
			return true;
		}
		else if(k<b[mid]){
			r=mid;
		}
		else if(k>b[mid]){
			l=mid;
		}
	}
	return false;
}
signed main(){
	ios::sync_with_stdio(false);
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	for(int i=1;i<=m;i++){
		cin>>b[i];
	}
	sort(b+1,b+n+1);
	for(int i=1;i<=n;i++){
		if(check(a[i],1,m)){
			cout<<a[i]<<' ';
		}
	}
	return 0;
}
//2 8 9
2024/1/14 17:33
加载中...