2023/03/10 机试学习记录

KY109 Zero-complexity Transposition

描述

You are given a sequence of integer numbers. Zero-complexity transposition of the sequence is the reverse of this sequence. Your task is to write a program that prints zero-complexity transposition of the given sequence.

输入描述

For each case, the first line of the input file contains one integer n-length of the sequence (0 < n ≤ 10 000). The second line contains n integers numbers-a1, a2, …, an (-1 000 000 000 000 000 ≤ ai ≤ 1 000 000 000 000 000).

思路

用stack来存储数字,输出时出栈

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

int main() {
    stack<long long> si1;
    int count;
    cin >> count;
    while (count--) {
        int temp;
        cin >> temp;
        si1.push(temp);
    }
    int number = si1.size();
    for (size_t i = 0; i < number; i++) {
        int temp = si1.top();
        cout << temp << " ";
        si1.pop();
    }
    return 0;
}

备注

1.如果用for (size_t i = 0; i < si1.size(); i++)会出现只输出3个数字的问题
原因是si1.size()会随si1元素出栈而减少
2.数字的范围是-1 000 000 000 000 000 ≤ ai ≤ 1 000 000 000 000 000 int无法完全表示
要用long long 类型

王道例题5.5 括号匹配

描述

在这里插入图片描述

思路

用stack解决问题
用stack存储左括号的下标
判断左括号时,可以先将其假设为非法,遍历到相应的右括号时再将其设置为合法

代码

#include<cstdio>
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<stack>
using namespace std;

int main() {
	string str1;
	string ret;//输出结果
	stack<int> s_int1;
	stack<char> s_char1;
	getline(cin, str1);
	for (size_t i = 0; i < str1.size(); i++) {
		if (str1[i] == '(') {
			s_int1.push(i);			//下标入栈
			ret.push_back('$');
		}
		else if (str1[i] == ')') {
			if (s_int1.empty()) {
				ret.push_back('?');
			}
			else {
				ret.push_back(' ');
				ret[s_int1.top()] = ' ';
				s_int1.pop();
			}
		}
		else
			ret.push_back(' ');
	}
	for (size_t i = 0; i < str1.size(); i++) {
		cout << ret[i];
	}
	return 0;
}

备注

输入输出时使用scanf、printf、fgets
效率高于cin、cout
不容易在机试时超时

KY180 堆栈的使用

描述

对于每组测试数据,第一行是一个正整数 n(0 < n <= 10000)。而后的 n 行,每行的第一个字符可能是’P’或者’O’或者’A’;如果是’P’,后面还会跟着一个整数,表示把这个数据压入堆栈;如果是’O’,表示将栈顶的值 pop 出来,如果堆栈中没有元素时,忽略本次操作;如果是’A’,表示询问当前栈顶的值,如果当时栈为空,则输出’E’。堆栈开始为空。

#include<cstdio>
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<stack>
using namespace std;

int main() {
	stack<int> s_int1;
	int count;
	string ret;
	while (cin >> count) {
		while (count--) {
			char type;
			int valve;
			while (cin >> type) {
				if (type == 'A') {
					if (s_int1.empty()) {
						cout << "E" << endl;
					}
					else {
						cout << s_int1.top() << endl;
					}
				}
				else if (type == 'P') {
					cin >> valve;
					s_int1.push(valve);
				}
				else if (type == 'O') {
					if (!s_int1.empty()) {
						s_int1.pop();
					}
				}
			}
		}
	}
	return 0;
}

KY129 简单计算器

描述

读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值。
测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运算符之间用一个空格分隔。没有非法表达式。当一行中只有0时输入结束,相应的结果不要输出。

思路

用两个stack分别存储数据与符号
用map来存储符号的优先级

代码

#include<cstdio>
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<stack>
#include<map>
using namespace std;

int main() {
	stack<char> s_operator;
	stack<double> s_number;
	string recive, nums;
	map<char, int> priority = {
		{'$', 0},
		{'+', 1},{'-', 1},
		{'*', 2},{'/', 2}
	};
	while (getline(cin, recive)) {
		if (recive == "0")
			break;
		recive.push_back('$');		//在字符串的结尾加上终止符
		for (size_t i = 0; i < recive.size(); i++) {
			if (recive[i] >= '0' && recive[i] <= '9') {
				nums.push_back(recive[i]);
			}
			else if (recive[i] == ' ') {
				if (nums != "") {
					s_number.push(stod(nums));	
					nums = "";//stod将string转换为double
				}
			}
			else {
				if (recive[i] == '$') {
					if (nums != "") {
						s_number.push(stod(nums));
						nums = "";
					}
				}
				while (!s_operator.empty() && priority[s_operator.top()] >= priority[recive[i]]) {
					char tempChar = s_operator.top();
					s_operator.pop();
					double temp1, temp2;
					temp2 = s_number.top();
					s_number.pop();
					temp1 = s_number.top();
					s_number.pop();
					switch (tempChar) {
						case '+':
							temp1 += temp2;
							s_number.push(temp1);
							break;
						case '-':
							temp1 -= temp2;
							s_number.push(temp1);
							break;
						case '*':
							temp1 *= temp2;
							s_number.push(temp1);
							break;
						case '/':
							temp1 /= temp2;
							s_number.push(temp1);
							break;
					}
				}
				s_operator.push(recive[i]);
			}
		}
		printf("%.2lf\n", s_number.top());
	}
	return 0;
}

备注

题目较难,与题目KY102相似

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值