pta数据结构day12

本文总结了八道编程题的解决思路与实现方法,包括回文串判断、数组操作、数字处理等,通过实例展示了使用C++进行问题解决的具体过程。

总结:用时2小时出头,写了 8道简单题,学到了一些处理方法;比如使用map,long long;

回文串,简单模拟题

#include<iostream>
#pragma warning(disable:4996)
using namespace std;

int main() {
	string ss;
	cin >> ss;
	int i;
	for (i = 0; i < ss.size()/2; i++) {//不需要等于
		if (ss[i] != ss[ss.size() - 1 - i]) {
			break;
		}
	}
	if (i < ss.size() / 2) {
		cout << "NO\n";
	}
	else {
		cout << "YES\n";
	}
	return 0;
}

题目详情 - 1009 说反话 (pintia.cn) 

 思路:输入数组中,再逆序输出即可;值得注意的是输入时用到EOF;

#include<iostream>
#include<vector>
#pragma warning(disable:4996)
using namespace std;

int main() {
	vector<string>v;
	string s;
	while (cin>>s&&!cin.eof()) {
		v.push_back(s);
	}
	for (vector<string>::iterator i = v.end() - 1; i != v.begin(); i--) {
		cout << *i << ' ';
	}
	cout << *v.begin();
	return 0;
}

题目详情 - 1011 A+B 和 C (pintia.cn) 

 idea:才知道long就是long int,都是32位,所以这题可能溢出,选择long long即64位整数,不会溢出;

#include<iostream>
using namespace std;

int main() {
	int n;
	long long a, b, c;
	int index = 1;
	cin >> n;
	while (n--) {
		cin >> a >> b >> c;
		cout << "Case #" << index << ": ";
		if (a + b > c) {
			cout << "true\n";
		}
		else {
			cout << "false\n";
		}
		++index;
	}
	return 0;
}

题目详情 - 1016 部分A+B (pintia.cn) 

没啥说的,好简单 

#include<iostream>
using namespace std;

int main() {
	int a, da, b, db;
	cin >> a >> da >> b >> db;
	int sum = 0,sum2=0;
	while (a) {
		if (a % 10 == da) {
			sum = 10 * sum + da;
		}
		a /= 10;
	}
	while (b) {
		if (b % 10 == db) {
			sum2 = 10 * sum2 + db;
		}
		b /= 10;
	}
	cout << sum + sum2;
	return 0;
}

 题目详情 - 1026 程序运行时间 (pintia.cn)

#include<iostream>
using namespace std;
#define CLK_TCK 100
int main() {
	int c1, c2;
	cin >> c1 >> c2;
	int time = 1.0*(c2 - c1)/CLK_TCK+0.5;//四舍五入
	int m = time / 60 % 60;
	int s = time % 60;
	int h = time / 60 / 60;
    printf("%02d:%02d:%02d",h,m,s);//2位
	return 0;
}

题目详情 - 1046 划拳 (pintia.cn) 

 

#include<iostream>
#include<vector>
#pragma warning(disable:4996)
using namespace std;

int main() {
	int n;
	int x, x1, y, y1;
	cin >> n;
	int ctn1 = 0, ctn2 = 0;
	for (int i = 0; i < n; i++) {
		cin >> x >> x1 >> y >> y1;
		if (x + y == x1&&x+y!=y1) {
			++ctn2;
		}
		if (x + y != x1 && x + y == y1) {
			++ctn1;
		}
	}
	cout << ctn1 <<' '<< ctn2;
	return 0;
}

题目详情 - 1008 数组元素循环右移问题 (pintia.cn) 

思路:这题值得一提,之前写过很多次,最近记录的是用三次翻转搞定,省事;一看书上答案,是我上个学期学的直接打印出来,抓住系统漏洞,太搞了;

#include<iostream>
#include<vector>
using namespace std;

void reverse(int arr[], int k) {
	for (int i = 0; i < k/2; i++) {
		int temp = arr[i];
		arr[i] = arr[k - i-1];
		arr[k - i-1] = temp;
	}
}
int main() {
	int n, k;
	cin >> n >> k;
    k%=n;//注意k>n的情况
	int arr[101];
	for (int i = 0; i < n; i++) {
		cin >> arr[i];
	}
	//3 reverse
	reverse(arr, n);
	reverse(arr, k);
	reverse(arr + k, n - k);
	//print
	for (int i = 0; i < n; i++) {
		if (i != 0) {
			cout << ' ';
		}
		cout << arr[i];
	}
	return 0;
}

偷鸡:

 

题目详情 - 1012 数字分类 (pintia.cn) 

思路:用2个数组储存,一个答案,一个是否出现此情况,方便后面的打印;用switch也使代码简单易读; 

#include<iostream>
#include<vector>
using namespace std;

int main() {
	int n,m,flag=1;
	cin >> n;
	int arr[5] = { 0 };
	int ctn[5] = { 0 };
	double A4 = 0;
	for (int i = 0; i < n; i++) {
		cin >> m;
		switch (m%5)
		{
		case 0:
			if (m % 2 == 0) {
				arr[0] += m;
				ctn[0]++;
			}
			break;
		case 1:
			arr[1] += flag * m;
			flag = -flag;
			ctn[1]++;
			break;
		case 2:
			++arr[2];
			ctn[2]++;
			break;
		case 3:
			A4 += m;
			++arr[3];
			ctn[3]++;
			break;
		case 4:
			ctn[4]++;
			if (arr[4] < m) {
				arr[4] = m;
			}
			break;
		default:
			break;
		}
	}
	for (int i = 0; i < 5; i++) {
		if (i != 0) {
			cout << ' ';
		}
		if (ctn[i] == 0) {
			cout << 'N';
		}
		else if(i==3) {
			printf("%.1f", A4 / arr[i]);
		}
		else {
			cout << arr[i];
		}
	}
	return 0;
}

 

 题目详情 - 1018 锤子剪刀布 (pintia.cn)

这题值得一写,主要是字符和数字的互相转换这一块 

#include<iostream>
#include<map>
using namespace std;

int main() {
	map<char, int>mp;//字符转成数字,按字母序排列
	mp['B'] = 0;//0>1,1>2,2>0---win: (c1+1)%3==c2 , same: c1=c2, lose: else
	mp['C'] = 1;
	mp['J'] = 2;
	char trans[3] = { 'B','C','J' };//数字转成字符
	int n;
	cin >> n;
	char c1,c2;
	int arr[3] = { 0 }, arr2[3] = { 0 };//胜负平次数
	int ch[3] = { 0 }, ch2[3] = { 0 };//bcj次数
	while (n--) {
		cin >> c1 >> c2;
		int x = mp[c1], y = mp[c2];
		if ((x + 1) % 3 == y) {
			++ch[x];//获胜手势次数加1
			++arr[0];//胜利加1
			++arr2[2];//另外的人失败加1
		}
		else if (x == y) {
			++arr[1];
			++arr2[1];
		}
		else {
			++ch2[y];
			++arr[2];
			++arr2[0];
		}
	}
	cout << arr[0] << ' ' << arr[1] << ' ' << arr[2] << endl;
	cout << arr2[0] << ' ' << arr2[1] << ' ' << arr2[2] << endl;
	int max1 = 0, max2 = 0;
	for (int i = 0; i < 3; i++) {
		if (ch[i] > ch[max1]) {
			max1 = i;
		}
		if (ch2[i] > ch2[max2]) {
			max2 = i;
		}
	}
	cout << trans[max1] << ' ' << trans[max2];
	return 0;
}

 

 

 

 

### 关于PTA前世档案系统的介绍 PTA(Programming Teaching Assistant)是一个用于编程教学和竞赛的在线评测平台。其前身可能涉及多个版本迭代以及功能扩展的历史记录。根据已知的信息,可以推测“前世档案”指的是该系统早期的设计理念、数据结构或者实现方式。 #### 系统的核心概念 在讨论PTA前世档案之前,需了解其基本运行机制。通常情况下,在线评测系统依赖树形结构或其他复杂的数据存储模型来处理多阶段的任务分配与结果反馈。例如,对于某些特定场景下的操作逻辑,“一直往左”的策略可能是为了优化访问路径[^1]。这种描述暗示了一种基于层次遍历或深度优先搜索的方式,适用于解决具有分层特性的计算问题。 另外,当提到具体比赛环境时,如2020年团体程序设计天梯赛中的题目设定,则进一步明确了实际应用场景下参数的作用范围[D, P]及其约束条件[^2]。这表明系统能够灵活调整配置以满足不同赛事需求。 #### 数据结构分析 针对上述提及的功能特点,以下是几个常见的核心组件: - **队列 (Queue)** 队列常被用来管理任务提交顺序,尤其是在需要按照时间先后执行的情况下非常有用。如果存在类似“市民两次获得口罩的时间至少需要间隔P天”的规则,则可以通过维护一个先进先出(FIFO) 的队列来跟踪每位用户的请求状态并施加相应限制。 - **二叉树/堆 (Binary Tree / Heap)** 如果涉及到查找最优解或是快速定位某个节点的操作,那么采用平衡二叉搜索树(BST) 或者最小(大)/最大堆(Max-Min Heap),可以帮助提高效率。特别是当目标是要找到最底层叶子结点的时候,“一直往左”或许正是指代沿着某一侧子树深入直至尽头的过程。 - **图(Graphs)** 更复杂的相互关系可以用图形表示出来。比如城市之间的连通情况或者其他网络拓扑结构都可以映射成节点间边的关系形式加以研究探讨。 ```python from collections import deque def simulate_mask_distribution(days, min_interval): queue = deque() result = [] for day in range(1, days + 1): while queue and queue[0] <= day - min_interval: queue.popleft() # Remove expired entries if not queue or len(queue) < max_allowed: # Assuming some limit exists queue.append(day) result.append(True) else: result.append(False) return result ``` 此代码片段展示了如何利用双端队列模拟发放过程,并考虑到了间隔期的要求。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值