oline judge Binary search 问题(急)! 在本地上调试通过,提交上去结果是 Wrong Answer
#include<iostream>usingnamespacestd;intbin(int*a,intn,intkey){intmid,front=0,back=n-1...
#include <iostream>
using namespace std;
int bin(int *a, int n, int key)
{
int mid, front=0, back=n-1;
while (front<=back) {
mid = (front+back)/2;
if (a[mid]==key)
return mid+1;
if (a[mid]<key)
front = mid+1;
else
back = mid-1;
}
return -1;
}
int main()
{
int a[100];
int n,key,i;
cin>>n>>key;
for(i=0;i<n;i++){ cin>>a[i];}
cout<<bin(a,n,key)<<endl;
return 0;
}
我的代码
http://info.zjfc.edu.cn/acm/problemDetail.aspx?pid=1335
以上是题目地址 展开
using namespace std;
int bin(int *a, int n, int key)
{
int mid, front=0, back=n-1;
while (front<=back) {
mid = (front+back)/2;
if (a[mid]==key)
return mid+1;
if (a[mid]<key)
front = mid+1;
else
back = mid-1;
}
return -1;
}
int main()
{
int a[100];
int n,key,i;
cin>>n>>key;
for(i=0;i<n;i++){ cin>>a[i];}
cout<<bin(a,n,key)<<endl;
return 0;
}
我的代码
http://info.zjfc.edu.cn/acm/problemDetail.aspx?pid=1335
以上是题目地址 展开
1个回答
展开全部
#include<iostream>
#include<cstdio>
using namespace std; int a[5000000]; int search(int n,int key) { int low=0,high=n,mid; while(low<=high) { mid=(low+high)/2; if(a[mid]==key) return mid+1; if(a[mid]<key) low=mid+1; else high=mid-1; } return -1; } int main() { int n,m,i; while(scanf("%d%d",&n,&m)==2) { for(i=0;i<n;i++) scanf("%d",&a[i]); printf("%d\n",search(n,m)); } }#include<iostream>
#include<cstdio>
using namespace std;
int a[5000000];
int search(int n,int key)
{
int low=0,high=n,mid;
while(low<=high)
{
mid=(low+high)/2;
if(a[mid]==key)
return mid+1;
if(a[mid]<key)
low=mid+1;
else
high=mid-1;
}
return -1;
}
int main()
{
int n,m,i;
while(scanf("%d%d",&n,&m)==2)
{
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("%d\n",search(n,m));
}
}
#include<cstdio>
using namespace std; int a[5000000]; int search(int n,int key) { int low=0,high=n,mid; while(low<=high) { mid=(low+high)/2; if(a[mid]==key) return mid+1; if(a[mid]<key) low=mid+1; else high=mid-1; } return -1; } int main() { int n,m,i; while(scanf("%d%d",&n,&m)==2) { for(i=0;i<n;i++) scanf("%d",&a[i]); printf("%d\n",search(n,m)); } }#include<iostream>
#include<cstdio>
using namespace std;
int a[5000000];
int search(int n,int key)
{
int low=0,high=n,mid;
while(low<=high)
{
mid=(low+high)/2;
if(a[mid]==key)
return mid+1;
if(a[mid]<key)
low=mid+1;
else
high=mid-1;
}
return -1;
}
int main()
{
int n,m,i;
while(scanf("%d%d",&n,&m)==2)
{
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("%d\n",search(n,m));
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询