2020-蓝桥杯校内模拟赛(省赛)

害 瞎写一通玩玩 不用放在心上

以下均为自己写题过程中的代码,不是最优解,也不是标准答案,欢迎大佬指正错误QAQ

我居然写着写着睡着了QAQ

A

括号匹配 我是把所有情况都枚举出来再判断
问题描述
  由1对括号,可以组成一种合法括号序列:()。
  由2对括号,可以组成两种合法括号序列:()()、(())。
  由4对括号组成的合法括号序列一共有多少种?
答案提交
  这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cstring>
#include <string>
#include <stack>

#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);

using namespace std;
// 长度一共为 8 
bool check(string str) {
    // 待匹配的左括号数量
    int left = 0;
    for (int i = 0;i < str.size() ;i ++) {
        char c = str[i];
		if (c == '(')
            left++;
        else // 遇到右括号
            left--;

        if (left < 0)
            return false;
    }
    return left == 0;
}
int a = 4 , b = 4;
void dfs(string s, int k)
{
	if(k > 8)return;
	if(k == 8)
	{
		if(check(s)){
			cout << s << endl;
		}
		return;	
	}
	if(a > 0)
	{
		a--;
		dfs(s + '(', k + 1);
		a++;
	}
	if(b > 0)
	{
		b--;
		dfs(s + ')', k + 1);
		b++;
	}	
} 
int main()
{
	SIS;	
	dfs("",0);	
	return 0;
} 

B

问题描述
  在计算机存储中,12.5MB是多少字节?
答案提交
  这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cstring>

#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);

int main()
{
		
	12.5 * 1024 *1024;
	return 0;
} 

C

暴力枚举
问题描述
  将LANQIAO中的字母重新排列,可以得到不同的单词,如LANQIAO、AAILNOQ等,注意这7个字母都要被用上,单词不一定有具体的英文意义。
  请问,总共能排列如多少个不同的单词。
答案提交
  这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cstring>
#include <map>

#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);

using namespace std;

string str = "LANQIAO";
bool vis[7];
int ans = 0;

map<string , bool> m;
void dfs(string s,int k)
{
	if(s.size() == str.size())
	{
		if(!m[s])
		{
			m[s] = 1;
			ans++;	
		}	
		return;
	}
	for(int i = 0;i < str.size() ;i ++)
	{
		if(!vis[i])
		{
			vis[i] = 1;
			dfs(s + str[i] , k + 1);
			vis[i] = 0;
		}
	}
}
int main()
{
	dfs("", 0);
	cout << ans << endl;
	return 0;
} 

D

问题描述
  一个包含有2019个结点的无向连通图,最少包含多少条边?
答案提交
  这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cstring>

#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);

using namespace std;

int main()
{
	2018		
	return 0;
} 

E

模拟
问题描述
  给定一个单词,请使用凯撒密码将这个单词加密。
  凯撒密码是一种替换加密的技术,单词中的所有字母都在字母表上向后偏移3位后被替换成密文。即a变为d,b变为e,...,w变为z,x变为a,y变为b,z变为c。
  例如,lanqiao会变成odqtldr。
输入格式
  输入一行,包含一个单词,单词中只包含小写英文字母。
输出格式
  输出一行,表示加密后的密文。
样例输入
lanqiao
样例输出
odqtldr
评测用例规模与约定
  对于所有评测用例,单词中的字母个数不超过100。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cstring>
#include <string>

#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);

using namespace std;

void work(string str)
{
	string t;
	int s = int('a');
	int e = int('z'); 
	for(int i = 0;i < str.size() ;i ++)
	{
		if(str[i] >= 'a' && str[i] <= 'z')
		{
			char ch = str[i];
			int tmp = int(ch) + 3;
			if(tmp > e)
			{
				tmp = tmp - e + s - 1;
			}
			t += (char)tmp;
		}else t += str[i];
	}	
	cout << t << endl;
}
int main()
{
	SIS;
	string s;
	getline(cin, s);
	work(s);
	return 0;
} 

F

模拟

问题描述
  给定三个整数 a, b, c,如果一个整数既不是 a 的整数倍也不是 b 的整数倍还不是 c 的整数倍,则这个数称为反倍数。
  请问在 1 至 n 中有多少个反倍数。
输入格式
  输入的第一行包含一个整数 n。
  第二行包含三个整数 a, b, c,相邻两个数之间用一个空格分隔。
输出格式
  输出一行包含一个整数,表示答案。
样例输入
30
2 3 6
样例输出
10
样例说明
  以下这些数满足要求:1, 5, 7, 11, 13, 17, 19, 23, 25, 29。
评测用例规模与约定
  对于 40% 的评测用例,1 <= n <= 10000。
  对于 80% 的评测用例,1 <= n <= 100000。
  对于所有评测用例,1 <= n <= 1000000,1 <= a <= n,1 <= b <= n,1 <= c <= n。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cstring>
#include <string>

#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);

using namespace std;

int n , a , b , c , ans;
int main()
{
	cin >> n;
	cin >> a >> b >> c;
	
	for(int i = 1;i <= n ;i ++)
	{
		if((i%a != 0) &&(i%b!=0) &&(i%c!=0))
		{
			ans++;
		}
	}
	cout << ans << endl;
	return 0;
} 

G

应该是个dp题 无奈太菜了不想去转换玄学方程了 太困了
问题描述
  如果一个序列的奇数项都比前一项大,偶数项都比前一项小,则称为一个摆动序列。即 a[2i]<a[2i-1], a[2i+1]>a[2i]。
  小明想知道,长度为 m,每个数都是 1 到 n 之间的正整数的摆动序列一共有多少个。
输入格式
  输入一行包含两个整数 m,n。
输出格式
  输出一个整数,表示答案。答案可能很大,请输出答案除以10000的余数。
样例输入
3 4
样例输出
14
样例说明
  以下是符合要求的摆动序列:
  2 1 2
  2 1 3
  2 1 4
  3 1 2
  3 1 3
  3 1 4
  3 2 3
  3 2 4
  4 1 2
  4 1 3
  4 1 4
  4 2 3
  4 2 4
  4 3 4
评测用例规模与约定
  对于 20% 的评测用例,1 <= n, m <= 5;
  对于 50% 的评测用例,1 <= n, m <= 10;
  对于 80% 的评测用例,1 <= n, m <= 100;
  对于所有评测用例,1 <= n, m <= 1000。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cstring>
#include <string>

#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);

using namespace std;
const int N = 1005;
const int mod = 10000;
int n , m;
// now 表示前一个是第几位
// num 表示当前数字是什么
int f[N][N];
int dfs(int now,int num)
{
	if(f[now][num] != 0)return f[now][num];
	if(now == m)return 1;
	int ans = 0;
	if(now & 1) // 如果前一位是奇数位 
	{
		for(int i = num - 1; i >= 1; i--)
		{
			ans =(ans + dfs(now + 1, i)) % mod;
		}	
	}else {
		for(int i = num + 1; i <= n ;i ++)
		{
			ans =(ans + dfs(now + 1, i)) % mod;
		}
	} 
	return f[now][num] = ans % mod;
}
int main()
{
	cin >> m >> n;
	int ans = 0;
	for(int i = 2;i <= n ;i ++)
	{
		ans = (ans + dfs(1 , i)) % mod;
	}
	cout << ans << endl;
	return 0;
} 

H

模拟

问题描述
  对于一个 n 行 m 列的表格,我们可以使用螺旋的方式给表格依次填上正整数,我们称填好的表格为一个螺旋矩阵。
  例如,一个 4 行 5 列的螺旋矩阵如下:
  1 2 3 4 5
  14 15 16 17 6
  13 20 19 18 7
  12 11 10 9 8
输入格式
  输入的第一行包含两个整数 n, m,分别表示螺旋矩阵的行数和列数。
  第二行包含两个整数 r, c,表示要求的行号和列号。
输出格式
  输出一个整数,表示螺旋矩阵中第 r 行第 c 列的元素的值。
样例输入
4 5
2 2
样例输出
15
评测用例规模与约定
  对于 30% 的评测用例,2 <= n, m <= 20。
  对于 70% 的评测用例,2 <= n, m <= 100。
  对于所有评测用例,2 <= n, m <= 1000,1 <= r <= n,1 <= c <= m。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cstring>
#include <string>

#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);

using namespace std;
const int N = 1005; 

int f[N][N];
int n , m , r , c;
void work()
{
	int k = n * m;
	int id = 1;
	// a 表示第一行
	// b 表示第一列
	// c 表示第 n 行
	// d 表示第 m 列 
	int a = 1, b = 1, c = n,d = m;
	while(id <= k)
	{
		// 从左往右
		for(int i = b; i <= d ;i ++)
		{
			f[a][i] = id;
			id++;	
		} a++;
		for(int i = a; i <= c ;i ++)
		{
			f[i][d] = id;
			id++;
		}d--;
		for(int i = d;i >= b ;i --)
		{
			f[c][i] = id;
			id++;
		}c--;
		for(int i = c ;i >= a; i--)
		{
			f[i][b] = id;
			id++;
		}b++;
	}
}
int main()
{
	cin >> n >> m >> r >> c;
	work();
	/*for(int i = 1;i <= n ;i ++)
	{
		for(int j = 1;j <= m ;j ++)
		{
			cout << f[i][j] << "\t";	
		}	
		cout << endl;
	}*/		
	cout << f[r][c];
	return 0;
} 

I

猜测是贪心 所以将树苗按照半径大小进行排序,优先考虑较大的树苗,将和树苗冲突的树苗都排除掉就是最终答案,注意最终结果去掉Π的 在中间计算过程直接计算好加上就好了

问题描述
  小明和朋友们一起去郊外植树,他们带了一些在自己实验室精心研究出的小树苗。
  小明和朋友们一共有 n 个人,他们经过精心挑选,在一块空地上每个人挑选了一个适合植树的位置,总共 n 个。他们准备把自己带的树苗都植下去。
  然而,他们遇到了一个困难:有的树苗比较大,而有的位置挨太近,导致两棵树植下去后会撞在一起。
  他们将树看成一个圆,圆心在他们找的位置上。如果两棵树对应的圆相交,这两棵树就不适合同时植下(相切不受影响),称为两棵树冲突。
  小明和朋友们决定先合计合计,只将其中的一部分树植下去,保证没有互相冲突的树。他们同时希望这些树所能覆盖的面积和(圆面积和)最大。
输入格式
  输入的第一行包含一个整数 n ,表示人数,即准备植树的位置数。
  接下来 n 行,每行三个整数 x, y, r,表示一棵树在空地上的横、纵坐标和半径。
输出格式
  输出一行包含一个整数,表示在不冲突下可以植树的面积和。由于每棵树的面积都是圆周率的整数倍,请输出答案除以圆周率后的值(应当是一个整数)。
样例输入
6
1 1 2
1 4 2
1 7 2
4 1 2
4 4 2
4 7 2
样例输出
12
评测用例规模与约定
  对于 30% 的评测用例,1 <= n <= 10;
  对于 60% 的评测用例,1 <= n <= 20;
  对于所有评测用例,1 <= n <= 30,0 <= x, y <= 1000,1 <= r <= 1000。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cstring>
#include <string>
#include <cmath>

#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);

using namespace std;
const int N = 1005;
int n;
struct node{
	int x, y , r;
}a[N];

bool cmp(node a,node b)
{
	return a.r > b.r;
}

double getdis(int sx,int sy,int ex,int ey)
{
	int a = (sx-ex)*(sx-ex);
	int b = (sy-ey)*(sy-ey);
	return sqrt(a + b);
}
bool vis[N]; 

int main()
{
	SIS;
	cin >> n;
	for(int i = 1;i <= n ;i ++)
	{
		cin >> a[i].x >> a[i].y >> a[i].r;		
	}		
	sort(a + 1, a + 1 + n , cmp);
	int ans = 0;
	// 优先种植半径最大的 
	for(int i = 1;i <= n ;i ++)
	{
		if(!vis[i])
		{	
			ans += a[i].r * a[i].r;
			for(int j = i + 1;j <= n ;j ++)
			{
				double dis = getdis(a[i].x,a[i].y,a[j].x,a[j].y);
				if(dis < a[i].r + a[j].r)// 表示这一棵树苗不能放
				{
					vis[j] = 1;		
				}	
			} 
		}
	}
	cout << ans << endl;
	return 0;
} 

J

最小生成树 但是需要自己构造边而已,应该大家一眼就能看出来最小生成树吧,但是有个坑卡了我好久 读题不仔细 我以为计算权值的时候要把h的差的平方也给放进去呢 坑了我好半天 于是我就睡着了。。。再读题才发现
问题描述
  2015年,全中国实现了户户通电。作为一名电力建设者,小明正在帮助一带一路上的国家通电。
  这一次,小明要帮助 n 个村庄通电,其中 1 号村庄正好可以建立一个发电站,所发的电足够所有村庄使用。
  现在,这 n 个村庄之间都没有电线相连,小明主要要做的是架设电线连接这些村庄,使得所有村庄都直接或间接的与发电站相通。
  小明测量了所有村庄的位置(坐标)和高度,如果要连接两个村庄,小明需要花费两个村庄之间的坐标距离加上高度差的平方,形式化描述为坐标为 (x_1, y_1) 高度为 h_1 的村庄与坐标为 (x_2, y_2) 高度为 h_2 的村庄之间连接的费用为
  sqrt((x_1-x_2)(x_1-x_2)+(y_1-y_2)(y_1-y_2))+(h_1-h_2)*(h_1-h_2)。
  在上式中 sqrt 表示取括号内的平方根。请注意括号的位置,高度的计算方式与横纵坐标的计算方式不同。
  由于经费有限,请帮助小明计算他至少要花费多少费用才能使这 n 个村庄都通电。
输入格式
  输入的第一行包含一个整数 n ,表示村庄的数量。
  接下来 n 行,每个三个整数 x, y, h,分别表示一个村庄的横、纵坐标和高度,其中第一个村庄可以建立发电站。
输出格式
  输出一行,包含一个实数,四舍五入保留 2 位小数,表示答案。
样例输入
4
1 1 3
9 9 7
8 8 6
4 5 4
样例输出
17.41
评测用例规模与约定
  对于 30% 的评测用例,1 <= n <= 10;
  对于 60% 的评测用例,1 <= n <= 100;
  对于所有评测用例,1 <= n <= 1000,0 <= x, y, h <= 10000。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cstring>
#include <string>
#include <cmath>

#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);

using namespace std;
const int N = 1005;
const int MAX = 0x3fffff;
struct node{
	int x, y ,h;
}a[N];

int n;
double e[N][N] , dis[N] , ans;
bool vis[N];

void prime()
{
    dis[1] = 0;
    for(int i = 1; i < n; i++)
    {
        int x = 0;
        for(int j = 1; j <= n; j++)
        {
            if(!vis[j] && (x == 0 || dis[j] < dis[x])) 
				x = j;
    	}
		vis[x] = 1;
        for(int y = 1; y <= n; y++)
		{
            if(!vis[y])
			{ 
				dis[y] = min(dis[y], e[x][y]);
			}
		}
    }
    for(int i = 2; i <= n; i++) ans += dis[i];
    printf("%.2f", ans);
}
double getdis(int sx, int sy ,int sh ,int ex,int ey,int eh)
{
	return sqrt((sx-ex)*(sx-ex) + (sy-ey)*(sy-ey)) + (sh-eh)*(sh-eh);
}

void getedge()
{
		// 计算出来每条边 
	for(int i = 1;i <= n - 1;i ++)
	{
		for(int j = i + 1;j <= n ;j ++)
		{
			double t = 
			getdis(a[i].x,a[i].y,a[i].h,a[j].x,a[j].y,a[j].h);
			e[i][j] = e[j][i] = min(e[i][j],t); 
		}
	}
}

void init()
{
	for(int i = 0;i <= n ;i ++)
	{
		for(int j = 0;j <= n ;j ++)
		{
			e[i][j] = MAX;
		}
	}
	for(int i = 0;i <= n ;i ++)dis[i] = MAX;
	
}
int main()
{
	SIS;
	cin >> n;
	init();
	for(int i = 1;i <= n ;i ++)
	{
		cin >> a[i].x >> a[i].y >> a[i].h;	
	}
	getedge();
	prime();
	return 0;
} 

猜你喜欢

转载自www.cnblogs.com/wlw-x/p/12699463.html