2020 团体程序设计天梯赛 题解+总结反思【未更新完】

L1-1 嫑废话上代码 (5分)

题意

直接输出

题解

#include <iostream>
using namespace std;
int main(){
	printf("Talk is cheap. Show me the code.\n");
}

L1-2 猫是液体 (5分)

题意

给你长宽高计算长方体体积

题解

#include <iostream>
using namespace std;
int main(){
	int h, l, w;
	cin >> l >> w >> h;
	cout << l * w * h << '\n';
}

L1-3 洛希极限 (10分)

题意

大背景下的简单计算,考察阅读题目的能力

题解

注意看题目要求的输出内容

#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
#include <string>
#include <string.h>
using namespace std;
int main(){
	double b1, b2, s;
	cin >> b1 >> s >> b2;
	double res = 0;
	if (s == 1){
		res = b1 * 1.26;
	} 
	else if (s == 0){
		res = b1 * 2.455;
	}
	printf("%.2f ", res);
	if ( (res - b2) <= 1e-6) cout <<"^_^\n";
	else cout << "T_T\n";
}

L1-4 调和平均 (10分)

题意

N 个正数的算数平均是这些数的和除以 N,它们的调和平均是它们倒数的算数平均的倒数。本题就请你计算给定的一系列正数的调和平均值。

题解

简单的来说,我们要求的是 a i ai ai 的倒数 的调和平均 ,再将这个结果再进行倒数

( 1 a 1 + 1 a 2 + 1 a 3 + . . . + 1 a n ) / n (\frac{1}{a_1} + \frac{1}{a_2} + \frac{1}{a_3} + ...+ \frac{1}{a_n}) / n (a11+a21+a31+...+an1)/n

然后再进行倒数

n / ( 1 a 1 + 1 a 2 + 1 a 3 + . . . + 1 a n ) n/(\frac{1}{a_1} + \frac{1}{a_2} + \frac{1}{a_3} + ...+ \frac{1}{a_n}) n/(a11+a21+a31+...+an1)

#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
#include <string>
#include <string.h>
using namespace std;
const int maxn = 1111;
double a[maxn];
double sum = 0;
int main(){
	int n; cin >> n;
	for(int i = 0; i < n; i++){
		double t; cin >> t; sum += 1.0 / t;
	}
	double ans = (double)n * 1.0 / sum * 1.0;
	printf("%.2f\n", ans);
}

L1-5 胎压监测 (15分)

题意

题解

#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
#include <string>
#include <string.h>
using namespace std;
const int maxn = 1111;
int p[10];
int pp[10];
int main(){
	int a[4];
	int min_tmp, pos, max_tmp;
	for(int i = 0; i < 4; i++) { cin >> a[i];
		if (i == 0) max_tmp = a[i];
		max_tmp = max(max_tmp, a[i]);
	}
	cin >> min_tmp >> pos;
	bool flag = true;int flag2 = 0;
	int k = 0;
	for(int i = 0; i < 4; i++){
		if (abs(a[i] - max_tmp) > pos|| a[i] < min_tmp)  p[k++] = i;
	}
	if (k <= 0){
		printf("Normal\n");
	}
	else if ( k == 1) {
		printf("Warning: please check #%d!\n", p[k - 1] + 1);
	}
	else {
		printf("Warning: please check all the tires!\n");
	}
}

L1-6 吃火锅 (15分)

题意

字符串匹配问题,统计要求字符出现位置,以及出现次数

题解

本题简单通过的话,就使用std库中的string.find函数能够直接查找,而不必使用kmp匹配

#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
#include <string>
#include <string.h>
using namespace std;
int main(){
	string s;
	int sum = 0;
	bool f = true;
	int fp = -1;
	int cnt = 0;
	while(1){
		getline(cin, s); 
		if (s == ".") break; 
		sum++;
		if (s.find("chi1 huo3 guo1") < s.size() && f) {fp = sum;cnt++;f=false;}
		else if (s.find("chi1 huo3 guo1") < s.size() && !f){cnt++;}
	}
	cout << sum << '\n';
	if (f == true) cout << "-_-#";
	else 
	cout << fp << " " << cnt << '\n';
}

L1-7 前世档案

题意

根据树形进行推理

题解

推理+二叉树(左节点 2root + 1 右节点 2 root + 2)

#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
#include <string>
#include <string.h>
using namespace std;
int main(){
	int n, m; cin >> n >> m;
	int t = pow(2, n);
	for(int i = 0; i < m; i++){
		string s; cin >> s;
		int root = 1;
		for(int i = 0; i < s.size(); i++){
			if (s[i] == 'y') root = root * 2;
			else root = root * 2 + 1;
		}
		cout << root - t + 1 << '\n';
	}
}

L1-8 刮刮彩票

题意

题解

根据题意进行模拟,正宗的模拟题

#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
#include <string>
#include <string.h>
#include <map>
using namespace std;
int ma[100];
int a[3][3];
int vis[10];
int main(){
ma[6] = 10000;
ma[7] = 36;
ma[8] = 720;
ma[9] = 360;
ma[10] = 80;
ma[11] = 252;
ma[12] = 108;
ma[13] = 72;
ma[14] = 54;
ma[15] = 180;
ma[16] = 72;
ma[17] = 180;
ma[18] = 119;
ma[19] = 36;
ma[20] = 306;
ma[21] = 1080;
ma[22] = 144;
ma[23] = 1800;
ma[24] = 3600;
	for(int i = 0; i < 3; i++){
		for(int j = 0; j < 3; j++){
			cin >> a[i][j];
			vis[a[i][j]] = 1;
		}
	}
	int tmp = -1;
	for(int i = 1; i <= 9; i++){
		if (vis[i] == 0){
			tmp = i;
			break;
		}
	}
	for(int i = 0; i < 3; i++){
		for(int j = 0; j < 3; j++){
			if (a[i][j] == 0) { a[i][j] = tmp;break;}
		}
	}
	for(int i = 0; i < 3; i++){
		int x, y; cin >> x >> y;
		cout << a[x - 1][y - 1] << '\n';
	}

	int j; cin >> j;
	if (j == 7) {
		//cout << a[0][0] + a[1][1] + a[2][2]<< '\n';
		cout << ma[a[0][0] + a[1][1] + a[2][2]] << '\n';
	}
	else if (j == 8) {
		cout << ma[a[0][2] + a[1][1] + a[2][0]]<< '\n';
	}
	else if (j <= 3){
		int sum = 0;
		for(int i = 0; i < 3; i++){
			sum += a[j - 1][i];
		}
		cout << ma[sum] << '\n';
	}
	else if (j > 3 && j < 7){
		j -= 4;
		int sum = 0;
		for(int i = 0; i < 3; i++) sum += a[i][j];
			cout << ma[sum] << '\n';
	}
}

L2-1 简单计算器

题意

本题要求你为初学数据结构的小伙伴设计一款简单的利用堆栈执行的计算器。如上图所示,计算器由两个堆栈组成,一个堆栈 S​1存放数字,另一个堆栈 S2​​ 存放运算符。计算器的最下方有一个等号键,每次按下这个键,计算器就执行以下操作:
从 S​1​​ 中弹出两个数字,顺序为 n1​​ 和 n​2​ ;
从 S​2 中弹出一个运算符 op;
执行计算 n​2​​ op n​1 ;
将得到的结果压回 S​1。
直到两个堆栈都为空时,计算结束,最后的结果将显示在屏幕上。

题解

实际上就是模拟运算的过程,需要使用到的数据结构是栈,栈可以使用std库里面的,也可以自己写
栈,如下

int sta[maxn];
int _top;
int val;
push:sta[_top++] = val;
top:sta[_top-1];
pop:_top--;

具体根据题意模拟计算即可

#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
#include <string>
#include <string.h>
#include <stack>
using namespace std;
stack<int> s1;
stack<char> s2;
int main(){
	int n; cin >> n;
	for(int i = 0; i < n; i++){
		int t; cin >> t; s1.push(t);
	}
	for(int i = 0; i < n - 1; i++){
		char c; cin >> c; s2.push(c);
	}
	while(!s2.empty()){
		int n1 = s1.top();
		s1.pop();
		int n2 = s1.top();
		s1.pop();
		char ch = s2.top();
		s2.pop();
		int res;
		if (ch == '+')
		{	
			res = n1 + n2;
			s1.push(res);
		}
		else if (ch == '-'){
			res = n2 - n1;
			s1.push(res);
		}
		else if (ch == '*'){
			res = n1 * n2;
			s1.push(res);
		}
		else if (ch == '/'){
			if (n1 == 0){
				printf("ERROR: %d/%d\n", n2, n1);
				return 0;
			}
			res = n2 / n1;
			s1.push(res);
		}
	}
	int ans = s1.top();
	cout << ans << '\n';
}

L2-2 口罩发放

题意

题解

  • 关键点有这么几个
  1. 身份证号得合理,必须是18位

L2-3 完全二叉树的层序遍历

L2-4 网红点打卡攻略

L3-1 那就别担心了

L3-2 传送门

L3-3 可怜的复杂度

总结反思

题目背景复杂的题目在考试中思考的时间有点过于久了,印象特别深刻,在看洛希极限的时候大脑一片模糊,第一遍直接跳过了,L1全部过完之后才去看洛希极限那一题,看了很久,在纸上才把题目内容想清楚,wa了不少遍,实际上并没有那么难,主要在于对题目的分析和理解吧。
在L1当中出现了一个小数学题,那题也让我有点印象,需要对进行约分,而不必每一步都进行操作,如果每一步都进行操作则会出现精度缺失的情况。
L2的口罩发放也印象十分深刻,细节很多的大模拟,也是我的硬伤,需要很多细节的处理,我在编写代码的过程中越写越乱,甚至有点写不下去的感觉。
后面的L2题主要考察的是有些表面考察的是数据结构实际是也是模拟,感觉天梯赛的题目还是主要以模拟,遇到不熟悉的数据结构可以尝试自己去进行模拟

  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值