求助大佬
查看原帖
求助大佬
1116551
liuweida0330楼主2023/12/13 13:07

为什么明明测试点对了却是0分

#include<bits/stdc++.h>
using namespace std;
long long a,b,c[100000000],d;
int sum(int x){
	int left=1,right=b,f;
	while(left<=right){
		f=(left+right)/2;
		if(c[f]==x){
			x=f;
			return x;
		}
		else if(c[f]>x){
			right=f-1;
		}
		else if(c[f]<x){
			left=f+1;
		}
		
	}
	
	return -1;
}
int main(){
	
	cin>>a>>b;
	for(int i=1;i<=a;i++){
		cin>>c[i];
	}
	for(int i=1;i<=b;i++){
		cin>>d;
		int ans=sum(d);
		cout<<ans<<" ";
	}
}
#include<bits/stdc++.h>
using namespace std;
long long a,b,c[100000000],d;
int sum(int x){
	int left=1,right=b,f;
	while(left<=right){
		f=(left+right)/2;
		if(c[f]==x){
			x=f;
			return x;
		}
		else if(c[f]>x){
			right=f-1;
		}
		else if(c[f]<x){
			left=f+1;
		}
		
	}
	
	return -1;
}
int main(){
	
	cin>>a>>b;
	for(int i=1;i<=a;i++){
		cin>>c[i];
	}
	for(int i=1;i<=b;i++){
		cin>>d;
		int ans=sum(d);
		cout<<ans<<" ";
	}
}

求助

2023/12/13 13:07
加载中...