欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

二分法及相关例题

程序员文章站 2023-12-27 08:16:15
...

1. 实现折半查找

二分法及相关例题

样例输入1

3 1
1 4 6
4

样例输出1

2

样例输入2

5 2
1 4 6 7 8
5 1

样例输出2

0 1

#include <iostream>
using namespace std;
#define max 1000005
int a[max];
int b[max];
int main() {
	int n, k;
	
	cin >> n >> k;
	for (int i = 1; i <= n; i++) {
		cin >> a[i]; 
	}
	for (int i = 1; i <= k; i++) {
		cin >> b[i];
	}
	int l, r;
	int mid, s;
	for (int i = 1; i <= k; i++) {
		  s = 0; l = 1; r = n;
		while (l <= r) {
			mid = (l + r) >> 1;
			if (a[mid] > b[i])  {
					r = mid - 1; 
			} else if (a[mid] < b[i]) {
					l = mid + 1;
			} else   {
				s = mid;
				break; 
			} 
		}
		cout << s;
		(i != k) && cout << " ";
	}	
	return 0;
} 

2. 是否可以求和

二分法及相关例题

样例输入1

5
1 2 3 4 5
-1 4

样例输出1

Yes

#include <iostream>
using namespace std;

int main() {
	long long n, a[100005];
	long long k, s;
	ios::sync_with_stdio(false);
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	cin >> k >> s;
	long long x = s - k;
	int l = 1, r = n, m = 0;
	int mid;
	while (l <= r) {
		mid = l + r >> 1;
		if (a[mid] > x) {
			r = mid - 1;
		} else if (a[mid] < x) {
			l = mid + 1;
		} else {
			m = mid;
			break;
		}
	}
	if (m == 0) {
		cout << "No" << endl;
	} else cout << "Yes" << endl;
	
	return 0;
} 

3. 两数之和

二分法及相关例题

样例输入1

5
1 2 3 4 5
4

样例输出1

Yes

样例输入2

5
1 2 4 4 5
4

样例输出2

No

#include <iostream>
using namespace std;

int main() {
	long long n, a[100005];
	long long s;
	ios::sync_with_stdio(false);
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	cin >> s;
	
	long long l = 1, r = n, m = 0;
	long long mid, x, y;
	for (int i = 1; i <= n; i++) {
		x = a[i];
		y = s - a[i];
		l = 1; r = n;
		m = 0;
		while (l < r) {
			mid = l + r >> 1;
			if (a[mid] > y) {
				r = mid - 1;
			} else if (a[mid] < y) {
				l = mid + 1;
			} else {
				m = mid;
				break;
			}
		}
		if (m != 0) {
				cout << "Yes" << endl;
				return 0;
		}
	}
	cout << "No" << endl;	
//	if (m == 0) {
//		cout << "No" << endl;
//	} else cout << "Yes" << endl;
	
	return 0;
} 

4.报数游戏

二分法及相关例题

样例输入1

5 5
1 5 10 15 20
3 6 12 18 24

样例输出1

1 5 10 15 20

#include<iostream>
using namespace std;
int a[100005];

int ans(int l, int r, int x) {
    while(l < r)  {
        int mid = l + r+1 >> 1;
        if(a[mid] <= x) l = mid;
        else r = mid - 1;
    }
    return a[l];
}
int main() {
    int n, m;
    cin >> n >> m;
    for(int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    int t;
    for(int i = 1; i <= m; i++) {
        cin >> t;
        if(i != m) cout << ans(1, n, t) << ' ';
        else cout << ans(1, n, t);
    }
    return 0;
}

5.二分法求解方程的根

二分法及相关例题

样例输入1

0

样例输出1

0.0000

样例输入2

1

样例输出2

0.5671

样例输入3

100

样例输出3

3.3856

#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
#define eps  1e-7
int a;
double fun(double l, double r) {
  while(r - l > eps) {
    double mid = (l + r) / 2;
    if(mid * exp(mid) > a) r = mid;
    else l = mid;
  }
  return l;
}
int main() {
    cin >> a;
    cout << fixed << setprecision(4) << fun(0, a) << endl;
    return 0;
}
相关标签: 算法学习 算法

上一篇:

下一篇: