二分查找大全
Posted 青少年信息学交流
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了二分查找大全相关的知识,希望对你有一定的参考价值。
这几天我将二分查找各种情况进行了分类整理,每种问题均提供while循环和递归2个代码,供朋友们参考!
1-1 查找目标值有没出现(循环):
#include <iostream>
#include <cstdio>
using namespace std;
int a[100005];
bool findnum(int left,int right,int key)
{
while(left<=right)
{
int mid=left+(right-left)/2;
if(a[mid]==key) return true;
else if(a[mid]<key) left=mid+1;
else right=mid-1;
}
return false;
}
int main()
{
int n,key;
cin>>n>>key;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
if(findnum(1,n,key)) cout<<"找到!"<<endl;
else cout<<"该数未出现!"<<endl;
return 0;
}
/*
10 6
2 3 3 5 5 5 6 8 11 9999
10 66666
2 3 3 5 5 5 6 8 11 9999
10 9
2 3 3 5 5 5 6 8 11 9999
10 1
2 3 3 5 5 5 6 8 11 9999
10 3
3 3 3 5 5 5 6 8 11 9999
*/
1-2 查找目标值有没出现(递归):
#include <iostream>
#include <cstdio>
using namespace std;
int a[100005];
bool findnum(int left,int right,int key)
{
if(left<=right)
{
int mid=left+(right-left)/2;
if(key==a[mid]) return true;
else if(key>a[mid]) return findnum(mid+1,right,key);
else if(key<a[mid]) return findnum(left,mid-1,key);
}
else return false;
}
int main()
{
int n,key;
cin>>n>>key;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
if(findnum(1,n,key)) cout<<"找到!"<<endl;
else cout<<"该数未出现!"<<endl;
return 0;
}
/*
10 6
2 3 3 5 5 5 6 8 11 9999
10 66666
2 3 3 5 5 5 6 8 11 9999
10 9
2 3 3 5 5 5 6 8 11 9999
10 1
2 3 3 5 5 5 6 8 11 9999
10 3
2 3 3 5 5 5 6 8 11 9999
10 3
3 3 3 5 5 5 6 8 11 9999
*/
2-1 查找和目标值第一个相等的元素(返回下标)(循环):
#include <iostream>
#include <cstdio>
using namespace std;
int a[100005];
int findk(int left,int right,int key)
{
while(left<right)
{
int mid=left+(right-left)/2;
if(key==a[mid]) right=mid;
else if(a[mid]<key) left=mid+1;
else if(a[mid]>key) right=mid-1;
}
if(a[left]==key) return left;
else return -1;
}
int main()
{
int n,key;
cin>>n>>key;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
cout<<findk(1,n,key)<<endl;
return 0;
}
/*
10 6
2 3 3 5 5 5 6 8 11 9999
10 66666
2 3 3 5 5 5 6 8 11 9999
10 9
2 3 3 5 5 5 6 8 11 9999
10 1
2 3 3 5 5 5 6 8 11 9999
10 3
2 3 3 5 5 5 6 8 11 9999
10 3
3 3 3 5 5 5 6 8 11 9999
*/
2-2 查找和目标值第一个相等的元素(返回下标)(递归):
#include <iostream>
#include <cstdio>
using namespace std;
int a[100005];
int findk(int left,int right,int key)
{
if(left<right)
{
int mid=left+(right-left)/2;
if(key==a[mid]) findk(left,mid,key);
else if(a[mid]<key) findk(mid+1,right,key);
else if(a[mid]>key) findk(left,mid-1,key);
}
else
{
if(a[left]==key) return left;
return -1;
}
}
int main()
{
int n,key;
cin>>n>>key;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
cout<<findk(1,n,key)<<endl;
return 0;
}
/*
10 6
2 3 3 5 5 5 6 8 11 9999
10 66666
2 3 3 5 5 5 6 8 11 9999
10 9
2 3 3 5 5 5 6 8 11 9999
10 1
2 3 3 5 5 5 6 8 11 9999
10 3
2 3 3 5 5 5 6 8 11 9999
10 3
3 3 3 5 5 5 6 8 11 9999
*/
3-1 查找和目标值最后一个相等的元素(返回下标)(循环):
#include <iostream>
#include <cstdio>
using namespace std;
int a[100005];
int findk(int left,int right,int key)
{
while(left<right-1)
{
int mid=left+(right-left)/2;
if(key==a[mid]) left=mid;
else if(a[mid]<key) left=mid+1;
else if(a[mid]>key) right=mid-1;
}
if(a[left]==key) return left;
if(a[right]==key) return right;
return -1;
}
int main()
{
int n,key;
cin>>n>>key;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
cout<<findk(1,n,key)<<endl;
return 0;
}
/*
10 6
2 3 3 5 5 5 6 8 11 9999
10 66666
2 3 3 5 5 5 6 8 11 9999
10 9
2 3 3 5 5 5 6 8 11 9999
10 1
2 3 3 5 5 5 6 8 11 9999
10 3
2 3 3 5 5 5 6 8 11 9999
10 3
3 3 3 5 5 5 6 8 11 9999
*/
3-2 查找和目标值最后一个相等的元素(返回下标)(递归):
#include <iostream>
#include <cstdio>
using namespace std;
int a[100005];
int findk(int left,int right,int key)
{
if(left<right-1)
{
int mid=left+(right-left)/2;
if(key==a[mid]) findk(mid,right,key);
else if(a[mid]<key) findk(mid+1,right,key);
else if(a[mid]>key) findk(left,mid-1,key);
}
else
{
if(a[left]==key) return left;
if(a[right]==key) return right;
return -1;
}
}
int main()
{
int n,key;
cin>>n>>key;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
cout<<findk(1,n,key)<<endl;
return 0;
}
/*
10 6
2 3 3 5 5 5 6 8 11 9999
10 66666
2 3 3 5 5 5 6 8 11 9999
10 9
2 3 3 5 5 5 6 8 11 9999
10 1
2 3 3 5 5 5 6 8 11 9999
10 3
2 3 3 5 5 5 6 8 11 9999
10 3
3 3 3 5 5 5 6 8 11 9999
*/
4-1 查找第一个大于目标值的元素(返回下标)(循环):
#include <iostream>
#include <cstdio>
using namespace std;
int a[1005];
int f(int left,int right,int key)
{
while(left<right-1)
{
int mid=left+(right-left)/2;
if(key==a[mid]) left=mid+1;
else if(key>a[mid]) left=mid+1;
else right=mid;
}
if(a[left]>key) return left;
if(a[right]>key) return right;
return -1;
}
int main()
{
int n,key;
cin>>n>>key;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
cout<<f(1,n,key)<<endl;
return 0;
}
/*
10 6
2 3 3 5 5 5 6 8 11 9999
10 66666
2 3 3 5 5 5 6 8 11 9999
10 9
2 3 3 5 5 5 6 8 11 9999
10 1
2 3 3 5 5 5 6 8 11 9999
10 3
2 3 3 5 5 5 6 8 11 9999
10 3
3 3 3 5 5 5 6 8 11 9999
*/
4-2 查找第一个大于目标值的元素(返回下标)(递归):
#include <iostream>
#include <cstdio>
using namespace std;
int a[1005];
int findnum(int left,int right,int key)
{
if(left<right-1)
{
int mid=left+(right-left)/2;
if(key==a[mid]) findnum(mid+1,right,key);
else if(key>a[mid]) findnum(mid+1,right,key);
else findnum(left,mid,key);
}
else
{
if(a[left]>key) return left;
if(a[right]>key) return right;
return -1;
}
}
int main()
{
int n,key;
cin>>n>>key;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
cout<<findnum(1,n,key)<<endl;
return 0;
}
/*
10 6
2 3 3 5 5 5 6 8 11 9999
10 66666
2 3 3 5 5 5 6 8 11 9999
10 9
2 3 3 5 5 5 6 8 11 9999
10 1
2 3 3 5 5 5 6 8 11 9999
10 3
2 3 3 5 5 5 6 8 11 9999
10 3
3 3 3 5 5 5 6 8 11 9999
*/
5-1 查找最后一个小于目标值的元素(返回下标)(循环):
#include <iostream>
#include <cstdio>
using namespace std;
int a[1005];
int findnum(int left,int right,int key)
{
while(left<right-1)
{
int mid=left+(right-left)/2;
if(key==a[mid]) right=mid-1;//
else if(key>a[mid]) left=mid;
else if(key<a[mid]) right=mid-1;
}
if(a[right]<key) return right;
if(a[left]<key) return left;
return -1;
}
int main()
{
int n,key;
cin>>n>>key;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
cout<<findnum(1,n,key)<<endl;
return 0;
}
/*
10 6
2 3 3 5 5 5 6 8 11 9999
10 66666
2 3 3 5 5 5 6 8 11 9999
10 9
2 3 3 5 5 5 6 8 11 9999
10 1
2 3 3 5 5 5 6 8 11 9999
10 3
2 3 3 5 5 5 6 8 11 9999
10 3
3 3 3 5 5 5 6 8 11 9999
*/
5-2 查找最后一个小于目标值的元素(返回下标)(递归):
#include <iostream>
#include <cstdio>
using namespace std;
int a[1005];
int findnum(int left,int right,int key)
{
if(left<right-1)
{
int mid=left+(right-left)/2;
if(key==a[mid]) findnum(left,mid-1,key);
else if(a[mid]>key) findnum(left,mid-1,key);
else if(a[mid]<key) findnum(mid,right,key);
}
else
{
if(a[right]<key) return right;
if(a[left]<key) return left;
return -1;
}
}
int main()
{
int n,key;
cin>>n>>key;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
cout<<findnum(1,n,key)<<endl;
return 0;
}
/*
10 6
2 3 3 5 5 5 6 8 11 9999
10 66666
2 3 3 5 5 5 6 8 11 9999
10 9
2 3 3 5 5 5 6 8 11 9999
10 1
2 3 3 5 5 5 6 8 11 9999
10 3
2 3 3 5 5 5 6 8 11 9999
10 3
3 3 3 5 5 5 6 8 11 9999
*/
6-1 查找第一个大于或等于目标值的元素(返回下标)(循环):
#include <iostream>
#include <cstdio>
using namespace std;
int a[1005];
int findnum(int left,int right,int key)
{
while(left<right-1)
{
int mid=left+(right-left)/2;
if(a[mid]==key) right=mid;
else if(a[mid]<key) left=mid+1;
else if(a[mid]>key) right=mid;
}
if(a[left]>=key) return left;
if(a[right]>=key) return right;
return -1;
}
int main()
{
int n,key;
cin>>n>>key;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
cout<<findnum(1,n,key)<<endl;
return 0;
}
/*
10 6
2 3 3 5 5 5 6 8 11 9999
10 66666
2 3 3 5 5 5 6 8 11 9999
10 9
2 3 3 5 5 5 6 8 11 9999
10 1
2 3 3 5 5 5 6 8 11 9999
10 3
2 3 3 5 5 5 6 8 11 9999
10 3
3 3 3 5 5 5 6 8 11 9999
*/
6-2 查找第一个大于或等于目标值的元素(返回下标)(递归):
#include <iostream>
#include <cstdio>
using namespace std;
int a[1005];
int findnum(int left,int right,int key)
{
if(left<right-1)
{
int mid=left+(right-left)/2;
if(a[mid]==key) findnum(left,mid,key);
else if(a[mid]<key) findnum(mid+1,right,key);
else if(a[mid]>key) findnum(left,mid,key);
}
else
{
if(a[left]>=key) return left;
if(a[right]>=key) return right;
return -1;
}
}
int main()
{
int n,key;
cin>>n>>key;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
cout<<findnum(1,n,key)<<endl;
return 0;
}
/*
10 6
2 3 3 5 5 5 6 8 11 9999
10 66666
2 3 3 5 5 5 6 8 11 9999
10 9
2 3 3 5 5 5 6 8 11 9999
10 1
2 3 3 5 5 5 6 8 11 9999
10 3
2 3 3 5 5 5 6 8 11 9999
10 3
3 3 3 5 5 5 6 8 11 9999
*/
7-1 查找最后一个小于或等于目标值的元素(返回下标)(循环):
#include <iostream>
#include <cstdio>
using namespace std;
int a[1005];
int findnum(int left,int right,int key)
{
while(left<right-1)
{
int mid=left+(right-left)/2;
if(a[mid]==key) left=mid;
else if(a[mid]>key) right=mid-1;
else if(a[mid]<key) left=mid;
}
if(a[right]<=key) return right;
if(a[left]<=key) return left;
return -1;
}
int main()
{
int n,key;
cin>>n>>key;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
cout<<findnum(1,n,key)<<endl;
return 0;
}
/*
10 6
2 3 3 5 5 5 6 8 11 9999
10 66666
2 3 3 5 5 5 6 8 11 9999
10 9
2 3 3 5 5 5 6 8 11 9999
10 1
2 3 3 5 5 5 6 8 11 9999
10 3
2 3 3 5 5 5 6 8 11 9999
10 3
3 3 3 5 5 5 6 8 11 9999
*/
7-2 查找最后一个小于或等于目标值的元素(返回下标)(递归):
#include <iostream>
#include <cstdio>
using namespace std;
int a[1005];
int findnum(int left,int right,int key)
{
if(left<right-1)
{
int mid=left+(right-left)/2;
if(a[mid]==key) findnum(mid,right,key);
else if(a[mid]>key) findnum(left,mid-1,key);
else if(a[mid]<key) findnum(mid,right,key);
}
else
{
if(a[right]<=key) return right;
if(a[left]<=key) return left;
return -1;
}
}
int main()
{
int n,key;
cin>>n>>key;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
cout<<findnum(1,n,key)<<endl;
return 0;
}
/*
10 6
2 3 3 5 5 5 6 8 11 9999
10 66666
2 3 3 5 5 5 6 8 11 9999
10 9
2 3 3 5 5 5 6 8 11 9999
10 1
2 3 3 5 5 5 6 8 11 9999
10 3
2 3 3 5 5 5 6 8 11 9999
10 3
3 3 3 5 5 5 6 8 11 9999
*/
8-1 查找最接近目标值的元素(循环):
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int a[1005];
int findnum(int left,int right,int key)
{
while(left<right-1)
{
int mid=left+(right-left)/2;
if(a[mid]==key) return mid;
else if(a[mid]<key) left=mid;
else if(a[mid]>key) right=mid;
}
if(abs(a[left]-key)<=abs(a[right]-key)) return left;
else return right;
}
int main()
{
int n,key;
cin>>n>>key;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
cout<<a[findnum(1,n,key)]<<endl;
return 0;
}
/*
10 6
2 3 3 5 5 5 6 8 11 9999
10 66666
2 3 3 5 5 5 6 8 11 9999
10 9
2 3 3 5 5 5 6 8 11 9999
10 1
2 3 3 5 5 5 6 8 11 9999
10 3
2 3 3 5 5 5 6 8 11 9999
10 3
3 3 3 5 5 5 6 8 11 9999
*/
8-2 查找最接近目标值的元素(递归)
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int a[1005];
int findnum(int left,int right,int key)
{
if(left<right-1)
{
int mid=left+(right-left)/2;
if(a[mid]==key) return mid;
else if(a[mid]>key) findnum(left,mid,key);
else if(a[mid]<key) findnum(mid,right,key);
}
else
{
if(abs(a[left]-key)<=abs(a[right]-key)) return left;
else return right;
}
}
int main()
{
int n,key;
cin>>n>>key;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
cout<<a[findnum(1,n,key)]<<endl;
return 0;
}
/*
10 6
2 3 3 5 5 5 6 8 11 9999
10 66666
2 3 3 5 5 5 6 8 11 9999
10 9
2 3 3 5 5 5 6 8 11 9999
10 1
2 3 3 5 5 5 6 8 11 9999
10 3
2 3 3 5 5 5 6 8 11 9999
10 3
3 3 3 5 5 5 6 8 11 9999
*/
更多精彩内容,请扫描二维码关注!
提高计算思维,编程创造未来!
以上是关于二分查找大全的主要内容,如果未能解决你的问题,请参考以下文章