#include<iostream> using namespace std; int main(){ int n,k; cin>>n>>k; int a[n]; for (int i=0; i<n; i++) cin>>a[i]; for (int i=0; i<k; i++){ int x; cin>>x; int low = 0, high = n - 1, ans = 0; while (low<=high){ int mid = (low+high)/2; if (a[mid]==x){ ans = mid+1; break; } else if (a[mid]<x){ low = mid+1; } else{ high = mid-1; } } cout<<ans<<endl; } return 0; }
以上程式碼純粹參考