本文介绍: 【代码】寒假2 蓝桥杯-二分。

#include<iostream>
using namespace std;
#include<algorithm>
int n;
int arr[100010];

int f_left(int q)
{
	int a = -1;
	int left = 1, right = n;
	int mid;
	while (left < right)
	{
		mid = (left + right) / 2;
		if (arr[mid] == q)
		{
			right = mid - 1;
			//cout << "1";
		}
		else if (arr[mid] < q)
		{
			left = mid+1;
			//cout << "2";
		}
		else
		{
			right = mid - 1;
			//cout << "3";
		}
	}
	return left;
}

int f_right(int q)
{
	int a = -1;
	int left = 1, right = n;
	int mid;
	while (left < right)
	{
		mid = (left + right) / 2;
		if (arr[mid] == q)
		{
			left = mid + 1;
			//cout << "4";
		}
		else if (arr[mid] > q)
		{
			
			right = mid - 1;
			//cout << "5";
		}
		else
		{
			left = mid + 1;
			//cout << "6";
		}
	}
	return right;
}
int main()
{
	int q;
	cin >> n >> q;
	for (int i = 1;i <= n;i++)
	{
		cin >> arr[i];
	}
	int left = f_left(q);
	int right = f_right(q);
	if (left ==-1&&right==-1)
	{
		cout << "-1 -1" << endl;
	}
	else
	{
		cout << left << " " << right << endl;
		cout << right - left  << endl;
	}
	return 0;
}

 

 

 

#include<iostream>
using namespace std;
int n;
int arr[60];
void f(int q)
{
	int left = 1;
	int right = n;
	int mid;
	int ans;
	while (left < right)
	{
		mid = (left + right) / 2;
		if (arr[mid] < q)
		{
			if (arr[mid + 1] > q)
			{
				cout << mid+1 << endl;
				return;
			}
			left = mid + 1;
		}
		else
		{
			if (arr[mid - 1] < q)
			{
				cout << mid << endl;
				return;
			}
			right = mid - 1;
		}
	}
}
int main()
{
	cin >> n;
	for (int i = 1;i <= n;i++)
	{
		cin >> arr[i];
	}
	int q;
	cin >> q;
	if (q < arr[1])
	{
		cout << 1 << endl;
	}
	else if (q > arr[n])
	{
		cout << n + 1 << endl;
	}
	else
	{
		f(q);
	}
	return 0;
}

 

#include<iostream>
using namespace std;
#include<algorithm>
int n, k;
int arr[100010];
int main()
{
	cin >> n >> k;
	for (int i = 1;i <= n;i++)
	{
		cin >> arr[i];
	}
	int left = 1;
	int right = 100000000;
	int mid;
	int ans = 0;
	while (left <= right)
	{
		int res = 0;
		mid = (left + right) / 2;
		for (int i = 1;i <= n;i++)
		{
			res += (arr[i] / mid);
			if (res > k)break;
		}
		if (res == k)
		{
			if (mid > ans)ans = mid;
			left = mid + 1;
		}
		else if (res > k)
		{
			if (mid > ans)ans = mid;
			left = mid + 1;
		}
		else
		{
			
			right = mid - 1;
		}
	}
	cout << ans << endl;
	return 0;
}

 

#include<iostream>
using namespace std;
int arr[27];
int main()
{
	int n;
	cin >> n;
	char ch;
	for (int i = 0;i < n;i++)
	{
		cin >> ch;
		if (int(ch) >= 65 && int(ch) <= 90)
		{
			arr[int(ch) - 64] = 1;
		}
		else
		{
			arr[int(ch) - 96] = 1;
		}
	}
	for (int i = 1;i <= 26;i++)
	{
		if (arr[i] != 1)
		{
			cout << "NO" << endl;
			return 0;
		}
	}
	cout << "YES" << endl;
	return 0;
}

 

原文地址:https://blog.csdn.net/weixin_72770922/article/details/135913024

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_65923.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注