寒假9-蓝桥杯训练

//轨道炮

cpp 复制代码
#include<iostream>
using namespace std;
#include<algorithm>
int logs[100010];
int main()
{
	int n;
	cin >> n;
	for (int i = 1;i <= n;i++)
	{
		cin >> logs[i];
	}
	sort(logs + 1, logs + n + 1);
	int ans = 1000000000;
	for (int i = 2;i <= n;i++)
	{
		if (logs[i] - logs[i - 1] < ans)ans = logs[i] - logs[i - 1];
	}
	int flag = 0;
	int res = 1;
	while (flag==0)
	{
		res = 1;
		for (int i = 2;i <= n;i++)
		{
			if ((logs[i] - logs[i - 1]) % ans == 0)
			{
				res += ((logs[i] - logs[i - 1]) / ans);
			}
			else
			{
				ans--;
				break;
			}
		}
		flag = 1;
	}
	cout << res << endl;
	return 0;
}
cpp 复制代码
#include<iostream>
using namespace std;
#include<string>
int arr[30];
int main()
{
	string str;
	cin >> str;
	for (int i = 0;i < str.length();i++)
	{
		arr[int(str[i]) - 96]+=1;
	}
	int ans1=0;
	int ans2;
	for (int i = 1;i <= 26;i++)
	{
		if (arr[i] > ans1)
		{
			ans2 = i;
			ans1 = arr[i];
		}
	}
	//cout << arr[15] << endl;
	cout << char(ans2 + 96) << endl << ans1 << endl;
	return 0;
}

//怒砍20分

cpp 复制代码
#include<iostream>
using namespace std;
char ch[30][30];
char chs[30][30];
int dx[] = { -1,1,0,0 };
int dy[] = { 0,0,-1,1 };
int n, m;
void dfs(int x, int y)
{
	chs[x][y] = '1';
	ch[x][y] = '0';
	for (int i = 0;i < 4;i++)
	{
		if (x + dx[i] >= 1 && y + dy[i] >= 1&& x + dx[i]<=n&& y + dy[i]<=m)
		{
			if (ch[x + dx[i]][y + dy[i]] == '1')
			{
				dfs(x + dx[i], y + dy[i]);
			}
		}
	}
}
int daan()
{
	int ans1 = 0;
	int ans2 = 0;
	for (int i = 1;i <= n;i++)
	{
		for (int j = 1;j <= m;j++)
		{
			if (chs[i][j] == '1')
			{
				if (i == 1 && j == 1)ans1++;
				else if (i == 1)
				{
					if (chs[i][j - 1] == '0')ans1++;
				}
				else if (j == 1)
				{
					if (chs[i - 1][j] == '0')ans1++;
				}
				else if (chs[i - 1][j] == '0' && chs[i][j - 1] == '0')ans1++;
			}
		}
	}
	for (int i = 1;i < n;i++)
	{
		if (chs[i + 1][m] == '0'&&chs[i][m]=='1')ans2++;
	}
	for (int i = 1;i < m;i++)
	{
		if (chs[n][i+1] == '0' && chs[n][i] == '1')ans2++;
	}
	if (chs[n][m] == '1')ans2++;
	//cout << "ans1=" << ans1 << " " << "ans2=" << ans2 << endl;
	return max(ans1, ans2);
}
int main()
{
	int ans = 0;
	cin >> n >> m;
	for (int i = 1;i <= n;i++)
	{
		for (int j = 1;j <= m;j++)
		{
			cin >> ch[i][j];
		}
	}
	for (int i = 1;i <= n;i++)
	{
		for (int j = 1;j <= m;j++)
		{
			if (ch[i][j]=='1')
			{
				for (int z = 1;z <= n;z++)
				{
					for (int w = 1;w <= m;w++)
					{
						chs[z][w] = '0';
					}
					
				}
				dfs(i, j);
				/*for (int z = 1;z <= n;z++)
				{
					for (int w = 1;w <= m;w++)
					{
						cout << chs[z][w];
					}
					cout << endl;
				}*/
				ans += daan();
			}
		}
	}
	cout << ans << endl;
	return 0;
}
cpp 复制代码
#include<iostream>
using namespace std;
#include<string>
int main()
{
	string str;
	cin >> str;
	int l = str.length();
	int ans = 0;
	for (int i = 0;i < l;i++)
	{
		ans += (int(str[i] - 48));
	}
	int flag = ans;
	while ((ans / 10) != 0)
	{
		flag = 0;
		while (ans)
		{
			flag += (ans % 10);
			ans /= 10;
		}
		ans = flag;
	}
	cout << flag << endl;
	return 0;
}
cpp 复制代码
#include<iostream>
using namespace std;
bool use[1000010];
int main()
{
	int n, m;
	cin >> n >> m;
	int maxx = max(n, m);
	int minn = min(n, m);
	int ans = minn-1;
	for (int i = 1;i <= n*m;i++)
	{
		if (i < minn)
		{
			use[i] = true;
			ans = i;
		}
		else
		{
			if (i % minn == 0 || i % maxx == 0)use[i] = false;
			else
			{
				if (i > maxx)
				{
					if (use[i - minn] == true && use[i - maxx] == true)
					{
						use[i] = true;
						ans = i;
					}
				}
				else if(use[i-minn])
				{
					use[i] = true;
					ans = i;
				}
			}
		}
	}
	cout << ans << endl;
	return 0;
}
cpp 复制代码
#include<iostream>
using namespace std;
int n, m;
int sum = 0;
bool use[15][15];
int arr[15][15];

int ans = 10000;

int dx[] = { 0 ,0,-1,1 };
int dy[] = { -1,1,0,0 };

void dfs(int x,int y,int  num, int d)
{
	//cout << num <<" "<< x<<" "<<y <<" "<< ans<< endl;
	if (num  > sum)return;
	else if (num  == sum)
	{
		if (d  < ans)ans = d ;
		return;
	}
	else if (d  >= ans)return;
	else 
	{
		use[x][y] = true;
		for (int i = 0;i < 4;i++)
		{
			if (x + dx[i] > 0 && x + dx[i] <= n && y + dy[i] > 0 && y + dy[i] <= m && use[x + dx[i]][y + dy[i]] == false)
			{
				dfs(x + dx[i], y + dy[i], num + arr[x+dx[i]][y+dy[i]], d + 1);
			}
		}
		use[x][y] = false;
	}
	return;
}
int main()
{//43
	cin >> m >> n;
	for (int i = 1;i <= n;i++)
	{
		for (int j = 1;j <= m;j++)
		{
			cin >> arr[i][j];
			sum += arr[i][j];
		}
	}
	if (sum % 2 != 0)
	{
		cout << 0 << endl;
	}
	else
	{
		sum /= 2;
		dfs(1, 1, arr[1][1], 1);
		if (ans == 10000)
		{
			cout << 0 << endl;
		}
		else
		{
			cout << ans << endl;
		}
	}
	return 0;
}
cpp 复制代码
#include<iostream>
using  namespace std;
#include<vector>
int n;
int ans = 0;
int arr[10];
bool use[10];
void f()
{
	//cout << 1 << endl;
	for (int i = 1;i <= 7;i++)
	{
		int flag1 = 1;
		int num1 = 0;
		while (flag1!=i+1)///1-5 1 12 123
		{
			num1 *= 10;
			num1 += arr[flag1];
			flag1++;
		}
		if (num1 >= n)break;
		for (int j = i + 1;j <= 8;j++)
		{
			int num2 = 0;
			int flag2 = i+1;
			while (flag2!=j+1)
			{
				num2 *= 10;
				num2 += arr[flag2];
				flag2++;
			}
			int num3 = 0;
			int flag3 = j+1;
			while (flag3 != 10)
			{
				num3 *= 10;
				num3 += arr[flag3];
				flag3++;
			}
			long long n1 = n * num3;
			long long n2 = num1 * num3 + num2;
			if (n1 == n2)ans++;
		}
	}
}

void dfs(int u)
{
	//cout << 2 << endl;
	if (u == 10)
	{
		f();
		return;
	}
	for (int i = 1;i <= 9;i++)
	{
		if (!use[i])
		{
			arr[u] = i;
			use[i] = true;
			dfs(u + 1);
			use[i] = false;
		}
	}
}

int main()
{
	cin >> n;
	dfs(1);
	cout << ans << endl;
	return 0;
}

//#include<iostream>
//#include <algorithm>
//using namespace std;
//int arr[9] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
//int get_num_helper(int a, int b)
//{
//	int tmp = 0;
//	while (a <= b)
//	{
//		tmp *= 10;
//		tmp += arr[a];
//		a++;
//	}
//	return tmp;
//}
//void get_num(int i, int j, int& a, int& b, int& c)
//{
//	a = get_num_helper(0, i);
//	b = get_num_helper(i + 1, j);
//	c = get_num_helper(j + 1, 8);
//}
//int main()
//{
//	int n;
// 	int ans = 0;
//	cin >> n;
//	// 1.给出1~9的所有排列9!
//	do {
//		// 2.对于每个排列进行划分:划分为整数a,分母b,分子c
//		int a, b, c;
//		for (int i = 0; i < 8; i++)
//		{
//			for (int j = i + 1; j < 9; j++)
//			{
//				get_num(i, j, a, b, c);
//				if (a == 0 || b == 0 || c == 0) continue;
//				// 3.检验划分后的结果是否等于给定数字n
//				if (b % c == 0 && a + (b / c) == n) ans++;
//			}
//		}
//
//	} while (next_permutation(arr, arr + 9));
//
//	cout << ans << endl;
//
//	return 0;
//}
cpp 复制代码
#include<iostream>
using namespace std;
int main()
{
	int a, b, c;
	cin >> a >> b >> c;
	int ans = a * b * c;
	for (int i = a * b * c;i >= 1;i--)
	{
		if ( i % a == 0 && i  % b == 0 && i % c == 0)
		{
			ans = i;
		}
	}
	cout << ans << endl;
	return 0;
}
相关推荐
算法歌者9 分钟前
[算法]入门1.矩阵转置
算法
林开落L24 分钟前
前缀和算法习题篇(上)
c++·算法·leetcode
远望清一色25 分钟前
基于MATLAB边缘检测博文
开发语言·算法·matlab
tyler_download27 分钟前
手撸 chatgpt 大模型:简述 LLM 的架构,算法和训练流程
算法·chatgpt
Prejudices37 分钟前
C++如何调用Python脚本
开发语言·c++·python
单音GG40 分钟前
推荐一个基于协程的C++(lua)游戏服务器
服务器·c++·游戏·lua
SoraLuna1 小时前
「Mac玩转仓颉内测版7」入门篇7 - Cangjie控制结构(下)
算法·macos·动态规划·cangjie
我狠狠地刷刷刷刷刷1 小时前
中文分词模拟器
开发语言·python·算法
鸽鸽程序猿1 小时前
【算法】【优选算法】前缀和(上)
java·算法·前缀和
qing_0406031 小时前
C++——多态
开发语言·c++·多态