题解
【基础】同时出现的数
1 条题解
-
0
#include<bits/stdc++.h> using namespace std; int a[100005], b[100005]; int n, m; // 二分查找是否存在 bool check(int x){ int l=0, r=n-1; while(l <= r){ int mid = (l+r)/2; if(x < a[mid]){ r = mid - 1; } else if(x == a[mid]){ return true; } else { l = mid + 1; } } return a[l] == x; } int main(){ cin >> n >> m; for(int i=0;i<n;i++) cin >> a[i]; for(int i=0;i<m;i++) cin >> b[i]; // 升序 sort(a, a+n); sort(b, b+m); // 判断是否存在 for(int i=0;i<m;i++){ if(check(b[i])){ cout << b[i] << " "; } } return 0; }
- 1