2 蓝桥杯打题记录

cpp 复制代码
#include<iostream>
using namespace std;
int n;
int k;
int const N = 1e5 + 10;
int arr[N];
long long s[N];
int main()
{
	cin >> n >> k;
	for (int i = 1;i <= n;i++)
	{
		cin >> arr[i];
		s[i] = s[i - 1] + arr[i];
	}
	long long ans = 0;
	for (int i = 1;i <= n;i++)
	{
		for (int j = i;j <= n;j++)
		{
			if ((s[j] - s[i - 1]) % k == 0)
			{
				ans++;
			}
		}
	}
	cout << ans << endl;
	return 0;
}
cpp 复制代码
#include<iostream>
using namespace std;
int n;
int k;
int const N = 1e5 + 10;
int arr[N];
long long s[N];
int x[N];
int main()
{
	cin >> n >> k;
	for (int i = 1;i <= n;i++)
	{
		cin >> arr[i];
		s[i] = s[i - 1] + arr[i];
	}
	long long ans = 0;
	for (int i = 0;i <= n;i++)
	{
		ans += x[s[i] % k];
		x[s[i] % k]++;
	}
	cout << ans << endl;
	return 0;
}
cpp 复制代码
#include<iostream>
using namespace std;
#include<algorithm>
#include<cstring>
#include<set>
#include<vector>
#include<string>
bool use[4];
int arr[4];
int s[4];
bool dp[2200][40][40];
void p(int x)
{
	if (x < 10)
	{
		cout << 0 << x;
	}
	else
	{
		cout << x;
	}
	return;
}
bool run(int x)
{
	if (x % 100 == 0)
	{
		if (x % 400 == 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	else
	{
		if (x % 4 == 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
}
void f(int num1,int num2,int num3)
{
	
	//月
	if (num2 > 12 || num2 < 1)
	{
		return;
	}
	//日
	{
		if (num2 == 1 || num2 == 3 || num2 == 5 || num2 == 7 || num2 == 8 || num2 == 10 || num2 == 12)
		{
			if (num3 <= 31)
			{
				cout << num1 << "-";
				p(num2);
				cout << "-";
				p(num3);
				cout<< endl;
				return;
			}
			else
			{
				return;
			}
		}
		else if (num2 == 2)
		{
			if (run(num1))
			{
				if (num3 <= 29)
				{
					cout << num1 << "-";
					p(num2);
					cout << "-";
					p(num3);
					cout << endl;
					return;
				}
				else
				{
					return;
				}
			}
			else
			{
				if(num3<=28)
				{
					cout << num1 << "-";
					p(num2);
					cout << "-";
					p(num3);
					cout << endl;
					return;
				}
				else
				{
					return;
				}
			}
		}
		else
		{
			if (num3 <= 30)
			{
				cout << num1 << "-";
				p(num2);
				cout << "-";
				p(num3);
				cout << endl;
				return;
			}
			else
			{
				return;
			}
		}
	}

}
//void dfs(int u)
//{
//	if (u == 4)
//	{
//		f();
//		return;
//	}
//	for (int i = 1;i <= 3;i++)
//	{
//		if (use[i])
//		{
//			s[u] = arr[i];
//			use[i] = false;
//			dfs(u+1);
//			use[i] = true;
//		}
//	}
//}
int fx(int num1)
{
	if (num1 >= 60)
	{
		num1 = num1 + 1900;
	}
	else
	{
		num1 += 2000;
	}
	return num1;
}
int main()
{
	memset(use, 1, sizeof(use));
	string str;
	cin >> str;
	/*arr[1] = int(str[0] - 48) + int(str[1] - 48);
	arr[2] = int(str[3] - 48) + int(str[4] - 48);
	arr[3] = int(str[6] - 48) + int(str[7] - 48);*/
	//dfs(1);
	int num1, num2, num3;
	num1 = int(str[0] - 48) + int(str[1] - 48);
	num2 = int(str[3] - 48) + int(str[4] - 48);
	num3 = int(str[6] - 48) + int(str[7] - 48);
	dp[fx(num1)][num2][num3] = 1;
	dp[fx(num3)][num1][num2] = 1;
	dp[fx(num3)][num2][num1] = 1;
	for (int i = 1960;i <= 2059;i++)
	{
		for (int j = 1;j <=12;j++)
		{
			for (int z = 1;z <= 31;z++)
			{
				if (dp[i][j][z])
				{
					f(i, j, z);
				}
			}
		}
	}
	return 0;
}
cpp 复制代码
#include<iostream>
using namespace std;
int const N = 1e3 + 10;
int m, n;
int k;
int fa[N * N];
int res[N * N];
int find(int x)
{
	if (fa[x] != x)
	{
		fa[x] = find(fa[x]);
	}
	return fa[x];
}
void unity(int x, int y)
{
	int f1 = find(x);
	int f2 = find(y);
	fa[f1] = f2;
}
int main()
{
	cin >> m>> n;
	cin >> k;
	for (int i = 1;i <= m;i++)
	{
		for (int j = 1;j <= n;j++)
		{
			fa[(i - 1) * n + j] = (i - 1) * n + j;
			//cout << (i - 1) * n + j << " ";
		}
		// << endl;
	}
	for (int i = 1;i <= k;i++)
	{
		int num1;
		int num2;
		cin >> num1 >> num2;
		int x = find(num1);
		int y = find(num2);
		unity(x, y);
	}
	int ans = 0;
	//验证
	for (int i = 1;i <= m;i++)
	{
		for (int j = 1;j <= n;j++)
		{
			res[find((i - 1) * n + j)]++;
			//cout << find((i - 1) * n + j) << " ";
		}
		//cout << endl;
	}
	for (int i = 1;i <= n * m;i++)
	{
		if (res[i] >= 1)ans++;
	}
	cout << ans << endl;
	return 0;
}
cpp 复制代码
#include<iostream>
using namespace std;
int const N = 1e3 + 10;
char arr[N][N];
char cop[N][N];
int n;
int dx[] = { 1,-1,0,0 };
int dy[] = { 0,0,1,-1 };
void yuan(int x, int y)
{
	cop[x][y] = '.';
	for (int i = 0;i < 4;i++)
	{
		int r1 = x + dx[i];
		int r2 = y + dy[i];
		if (r1 > 0 && r1 <= n && r2 > 0 && r2 <= n&&cop[r1][r2]=='#')
		{
			yuan(r1, r2);
		}
	}
}
void hou(int x,int y)
{
	arr[x][y] = '.';
	for (int i = 0;i < 4;i++)
	{
		int r1 = x + dx[i];
		int r2 = y + dy[i];
		if (r1 > 0 && r1 <= n && r2 > 0 && r2 <= n && arr[r1][r2] == '#')
		{
			hou(r1, r2);
		}
	}
}
int main()
{
	cin >> n;
	for (int i = 1;i <= n;i++)
	{
		for (int j = 1;j <= n;j++)
		{
			cin >> arr[i][j];
			cop[i][j] = arr[i][j];
		}
	}
	int ans = 0;
	for (int i = 1;i <= n;i++)
	{
		for (int j = 1;j <= n;j++)
		{
			if (cop[i][j] == '#')
			{
				ans++;
				yuan(i, j);
			}
		}
	}
	int anss = 0;
	for (int i = 1;i <= n;i++)
	{
		for (int j = 1;j <= n;j++)
		{
			if (arr[i][j] == '#')
			{
				int flag = 1;
				for (int z = 0;z < 4;z++)
				{
					int r1 = i + dx[z];
					int r2 = j + dy[z];
					if (r1 > 0 && r1 <= n && r2 > 0 && r2 <= n && arr[r1][r2] == '.')
					{
						flag = 0;
					}
				}
				if (flag == 1)
				{
					anss++;
					hou(i, j);
				}
			}
		}
	}
	cout << ans-anss << endl;
	return 0;
}
相关推荐
我是谁??17 分钟前
C/C++使用AddressSanitizer检测内存错误
c语言·c++
小码农<^_^>19 分钟前
优选算法精品课--滑动窗口算法(一)
算法
羊小猪~~21 分钟前
神经网络基础--什么是正向传播??什么是方向传播??
人工智能·pytorch·python·深度学习·神经网络·算法·机器学习
软工菜鸡1 小时前
预训练语言模型BERT——PaddleNLP中的预训练模型
大数据·人工智能·深度学习·算法·语言模型·自然语言处理·bert
南宫生1 小时前
贪心算法习题其三【力扣】【算法学习day.20】
java·数据结构·学习·算法·leetcode·贪心算法
发霉的闲鱼1 小时前
MFC 重写了listControl类(类名为A),并把双击事件的处理函数定义在A中,主窗口如何接收表格是否被双击
c++·mfc
小c君tt1 小时前
MFC中Excel的导入以及使用步骤
c++·excel·mfc
xiaoxiao涛1 小时前
协程6 --- HOOK
c++·协程
AI视觉网奇1 小时前
sklearn 安装使用笔记
人工智能·算法·sklearn
JingHongB2 小时前
代码随想录算法训练营Day55 | 图论理论基础、深度优先搜索理论基础、卡玛网 98.所有可达路径、797. 所有可能的路径、广度优先搜索理论基础
算法·深度优先·图论