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;
}
相关推荐
可涵不会debug7 分钟前
C语言文件操作:标准库与系统调用实践
linux·服务器·c语言·开发语言·c++
凭君语未可10 分钟前
豆包MarsCode:小C点菜问题
算法
C语言魔术师30 分钟前
【小游戏篇】三子棋游戏
前端·算法·游戏
自由自在的小Bird30 分钟前
简单排序算法
数据结构·算法·排序算法
刘好念34 分钟前
[OpenGL]实现屏幕空间环境光遮蔽(Screen-Space Ambient Occlusion, SSAO)
c++·计算机图形学·opengl·glsl
C嘎嘎嵌入式开发2 小时前
什么是僵尸进程
服务器·数据库·c++
王老师青少年编程7 小时前
gesp(C++五级)(14)洛谷:B4071:[GESP202412 五级] 武器强化
开发语言·c++·算法·gesp·csp·信奥赛
DogDaoDao7 小时前
leetcode 面试经典 150 题:有效的括号
c++·算法·leetcode·面试··stack·有效的括号
Coovally AI模型快速验证8 小时前
MMYOLO:打破单一模式限制,多模态目标检测的革命性突破!
人工智能·算法·yolo·目标检测·机器学习·计算机视觉·目标跟踪
一只小bit8 小时前
C++之初识模版
开发语言·c++