32分蒟蒻求助,其他全RE,有没有什么需要改的地方
查看原帖
32分蒟蒻求助,其他全RE,有没有什么需要改的地方
866521
jijijibeibeibei楼主2023/9/7 18:51
#include<iostream>//p2249查找 ,运行错误 
#include<cstring>
using namespace std;
int a[1000005],b[10005];//a待查询,b要查询 
int main(){
	 ios::sync_with_stdio(0);
    int n,m,mun;
    cin>>n>>m;//数字个数和询问次数 
    int c=1,d=n;//mun表中点,c是起点,d是终点,b[i]在c,d之间 
    for(int i=1;i<=n;i++){
        cin>>a[i];//待查询的数字 
    }
    for(int i=1;i<=m;i++){
        cin>>b[i];//需要查询的数字 
        c = 1; d = n;
        while(c<d){
            mun = c + (d-c) / 2;
            if( a[mun] < b[i] ) c=mun+1 ;//比中点大,找后半段 
            else d=mun;
        }
		if(a[c]==b[i])cout<<c<<" ";
        else cout<<-1<<" ";
    }
    return 0;
}
2023/9/7 18:51
加载中...