2025年北京航空航天大学计算机考研复试机试真题(解题思路 + AC 代码)
2025年北京航空航天大学计算机考研复试机试真题
2025年北京航空航天大学计算机考研复试上机真题
历年北京航空航天大学计算机考研复试上机真题
历年北京航空航天大学计算机考研复试机试真题
更多学校完整题目开源地址:https://gitcode.com/u014339447/pgcode
百度一下pgcode 即可查看,输入 “学校名称” 即可筛选该校历年机试真题,包括真题、ac代码、解题思路、视频讲解。

老鼠回家路-北京航空航天大学
题目描述
老鼠找食物,但是回家的时候找到最短路。
输入是 x x x- y y y, x x x 是 1 1 1 2 2 2 3 3 3 4 4 4 其中的一个,代表四个方向。
y y y 是向这个方向走的距离。
比如:
1 1 1- 2 2 2 表示,向上走两步
2 2 2- 3 3 3 向下走 3 3 3 步
3 3 3- 1 1 1 向左走 1 1 1 步
4 4 4- 2 2 2 向右走 2 2 2 步
0 0 0- 0 0 0 表示找到了
然后返回的时候,找到最短路径。
要求给他找回头路,把重复的路给去掉。
题目首先规定四个方向: 1 1 1、 2 2 2、 3 3 3、 4 4 4 分别代表上下左右。
输入序列形式为 1 1 1- 3 3 3 3 3 3- 4 4 4 1 1 1- 4 4 4…,前一个数字代表方向,后一个数字代表前进距离,以 0 0 0- 0 0 0 为结束,结束则代表老鼠找到了食物。
老鼠在碰到死路时会原路返回到分叉路口,探索下一个方向。
需要求解老鼠原路返回的最佳路径,以 2 2 2- 3 3 3 4 4 4- 2 2 2…等作为输出。
最佳路径的描述是“不走回头路”,即没有折返过程即可
输入样例
1-1 3-1 1-1 2-1 4-2 1-2 4-1 1-1 2-1 3-1 1-1 0-0
输出样例
2-3 3-1 2-1
#include <bits/stdc++.h>
using namespace std;
int main(){
int x, y;
char minus;
cin >> x >> minus >> y;
stack<int> direction;
stack<int> distance;
while(!(x == 0 && y == 0)){
if(direction.empty()){ //栈为空
direction.push(x);
distance.push(y);
}else{
int last_direction = direction.top();
int last_distance = distance.top();
if(last_direction == 1){
if(x == 1){
distance.pop();
distance.push(last_distance + y);
}else if(x == 2){
if(last_distance > y){
distance.pop();
distance.push(last_distance - y);
}else if(last_distance == y){ //回到上一个点了
distance.pop();
direction.pop();
}else{ //反向了
distance.pop();
direction.pop();
direction.push(2);
distance.push(y - last_distance);
}
}else{ //没有冲突,栈顶放置
direction.push(x);
distance.push(y);
}
}else if(last_direction == 2){
if(x == 2){
distance.pop();
distance.push(last_distance + y);
}else if(x == 1){
if(last_distance > y){
distance.pop();
distance.push(last_distance - y);
}else if(last_distance == y){ //回到上一个点了
distance.pop();
direction.pop();
}else{ //反向了
distance.pop();
direction.pop();
direction.push(1);
distance.push(y - last_distance);
}
}else{ //没有冲突,栈顶放置
direction.push(x);
distance.push(y);
}
}else if(last_direction == 3){
if(x == 3){
distance.pop();
distance.push(last_distance + y);
}else if(x == 4){
if(last_distance > y){
distance.pop();
distance.push(last_distance - y);
}else if(last_distance == y){ //回到上一个点了
distance.pop();
direction.pop();
}else{ //反向了
distance.pop();
direction.pop();
direction.push(4);
distance.push(y - last_distance);
}
}else{ //没有冲突,栈顶放置
direction.push(x);
distance.push(y);
}
}else{
if(x == 4){
distance.pop();
distance.push(last_distance + y);
}else if(x == 3){
if(last_distance > y){
distance.pop();
distance.push(last_distance - y);
}else if(last_distance == y){ //回到上一个点了
distance.pop();
direction.pop();
}else{ //反向了
distance.pop();
direction.pop();
direction.push(3);
distance.push(y - last_distance);
}
}else{ //没有冲突,栈顶放置
direction.push(x);
distance.push(y);
}
}
}
cin >> x >> minus >> y;
}
while(!direction.empty()){
int temp_direction = direction.top();
direction.pop();
int temp_distance = distance.top();
distance.pop();
if(temp_direction == 1){
cout << 2 << '-' << temp_distance << ' ';
}else if(temp_direction == 2){
cout << 1 << '-' << temp_distance << ' ';
}else if(temp_direction == 3){
cout << 4 << '-' << temp_distance << ' ';
}else{
cout << 3 << '-' << temp_distance << ' ';
}
}
return 0;
}
手机基站-北京航空航天大学
题目描述
一共 6 6 6 个手机基站,具有记录手机连接基站的能力。
6 6 6 个手机基站分别记为 A A A B B B C C C D D D E E E F F F,他们具有自己的覆盖范围且任何两个基站的覆盖范围不相交。
基站保存的手机登陆日志包括手机号( 11 11 11 位,用字符串保存)、基站编号、登陆时间( 6 6 6 位数字,用字符串保存)、登出时间( 6 6 6 位,用字符串保存)。
读入某一天多个基站的手机登陆日志信息和一个要查找的人员手机号,查找与该人员同时空的手机号。
输入格式
一个 N N N 和 N N N 条登陆日志信息,最后还有一个要查找人员的手机号。
输出格式
输出与要查找人员时间和地点有重叠的人员信息(即日志信息),依次输出手机号、基站编号、登陆时间和登出时间; 按照登陆时间进行排序,如果登陆时间相同按照手机号进行排序(如果一个人员的登出时间和另一个人员的登陆时间相同也算时间重叠)
输入样例
7
11111 A 080000 225959
22222 B 080000 225959
33333 A 100000 110000
44444 B 101000 110000
55555 A 120000 131000
66666 A 225959 235959
77777 A 100000 120000
11111
输出样例
33333 A 100000 110000
77777 A 100000 120000
55555 A 120000 131000
66666 A 225959 235959
#include <iostream>
#include <unordered_map>
#include <vector>
#include <algorithm>
using namespace std;
struct logInfo{
string number;
char type;
string login;
string logout;
};
bool compare(logInfo &a ,logInfo &b){
if(a.login != b.login) return a.login < b.login;
return a.number < b.number;
}
int main() {
int n;
cin >> n;
string number, login, logout;
char type;
unordered_map<string, logInfo> logMap;
for(int i = 0; i < n; i++){
cin >> number >> type >> login >>logout;
logMap[number] = {number, type, login, logout};
}
string target;
cin >> target;
vector<logInfo> result;
for(auto it : logMap){
logInfo &curLog = it.second;
if(it.first != target && it.second.type == logMap[target].type && it.second.login <= logMap[target].logout && it.second.logout >= logMap[target].login){
result.push_back(curLog);
}
}
sort(result.begin(), result.end(), compare);
for(auto it : result){
cout << it.number << " " << it.type << " " <<it.login << " " << it.logout << endl;
}
return 0;
}
字符串距离-北京航空航天大学
题目描述
在信息论汉明码中,存在一个定义:字符串之间的距离,指两个等长字符串进行比较时,存在不同字母的位置的个数。
例如 01010 01010 01010 和 01011 01011 01011 的距离是 1 1 1(最后一位不一样), R O S E S ROSES ROSES 和 r o s e s roses roses 的距离是 5 5 5(每一位的大小写都不一样)。
输入格式
一个整数 n n n( 2 ≤ n ≤ 16 2 \leq n \leq 16 2≤n≤16),后接 n n n 行相同长度的字符串,字符串两两互不相同。
输出格式
输出每对字符串两两比较的结果,输出格式如下:
较小的字符串 + + + 空格 + + + 较大的字符串 + + + 空格 + + + 两者的距离 + + + 换行符
优先输出距离最小的字符串组合,如果有的组合距离相同,则优先输出较小的字符串更小的组合,如果较小的字符串相同,则优先输出较大的字符串更小的组合。
如果比较结果多于 6 6 6 对,则只输出前 6 6 6 对。
PS: 字符串的大小指的是字符串的 A S C I I ASCII ASCII 码的字典序大小。
输入样例
7
01010
11011
10101
10011
Roses
roses
cotes
输出样例
10011 11011 1
Roses roses 1
01010 11011 2
10011 10101 2
Roses cotes 2
cotes roses 2
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
using namespace std;
struct resInfo{
string low_str;
string high_str;
int distance;
};
int cal(string a, string b){
int distance = 0;
char ch, sh;
for(int i = 0; i < a.size(); i++){
ch = a[i];
sh = b[i];
if(ch != sh) distance++;
}
return distance;
}
bool compare(resInfo a, resInfo b){
if(a.distance != b.distance) return a.distance < b.distance;
if(a.low_str != b.low_str) return a.low_str < b.low_str;
return a.high_str < b.high_str;
}
int main() {
int n;
cin >> n;
vector<string> data;
for(int i = 0; i < n; i++){
string a;
cin >> a;
data.push_back(a);
}
vector<resInfo> result;
for(int i = 0; i < n; i++){
for(int j = i + 1; j < n; j++){
int curDistance = cal(data[i], data[j]);
string curLow = min(data[i], data[j]);
string curHigh = max(data[i], data[j]);
result.push_back({curLow, curHigh, curDistance});
}
}
sort(result.begin(), result.end(), compare);
int res_n = min(6, static_cast<int>(result.size()));
for(int i = 0; i < res_n; i++){
cout << result[i].low_str << " " << result[i].high_str << " " << result[i].distance << endl;
}
return 0;
}
模拟编译系统-北京航空航天大学
题目描述
该程序会模拟一个解释运行的编译系统,每次从标准输入中读入一行指令,都要进行对应的操作,该程序中存在四种语句,语句最长 200 200 200 个字符,每个语句占且仅占一行,每行最多一个语句。
四种语句分别如下:
-
输入语句,格式为 r e a d read read + 空格 + < < < 变量序列 > > > + 换行符,变量序列是以空分隔的变量名称的组合,变量的名字只可能为单个的小写字母,变量不需要前声明,紧接着下一行,输入若干十进制整数,每个整数和上一行的变量对应赋值。
-
赋值语句,格式为 变量 变量 变量 + 等于符号 等于符号 等于符号 + 表达式 表达式 表达式 + 换行符,表达式中包含十进制整数,变量, + + + − - − ∗ * ∗ / / / ( ( ( ) ) ) 这六种计算符号,整个语句不包含空白符。
-
输出语句,格式为 p r i n t print print + 空格 + < < < 变量序列 > > > + 换行符,紧接着下一行,输出若干个变量的值,值以浮点数形式输出,保留两位小数,值之间以空格分隔,最后一个值后面不跟空格跟换行符。
-
结束语句,格式为 e x i t exit exit + 换行符,该语句后直接结束程序运行。
测试样例中,所有语句均不包含语法错误,所有变量在使用前均会赋值(不需要考虑输入错误的情况)。
输入格式
见题目
输出格式
见题目
输入样例
read a
10
b=20
c=(a+b)/4
print a b c
exit
输出样例
10.00 20.00 7.50
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <stack>
#include <map>
#include <cctype>
#include <iomanip>
using namespace std;
/**
* @class ExpressionEvaluator
* @brief 表达式求值器,支持 +, -, *, / 和括号
*/
class ExpressionEvaluator {
private:
map<string, double>& variables; // 变量表引用
// 检查是否为操作符
bool isOperator(char c) {
return c == '+' || c == '-' || c == '*' || c == '/';
}
// 获取操作符优先级
int precedence(char op) {
if (op == '+' || op == '-') return 1;
if (op == '*' || op == '/') return 2;
return 0;
}
// 执行计算
double applyOp(double a, double b, char op) {
switch (op) {
case '+': return a + b;
case '-': return a - b;
case '*': return a * b;
case '/': return a / b; // 题目保证不会除0
}
return 0;
}
public:
ExpressionEvaluator(map<string, double>& vars) : variables(vars) {}
/**
* @brief 计算表达式的值
* @param expr 中缀表达式字符串(不包含空格)
* @return 计算结果
*/
double evaluate(const string& expr) {
stack<char> ops; // 运算符栈
stack<double> vals; // 操作数栈
int i = 0;
int n = expr.length();
while (i < n) {
// 跳过空格(虽然表达式没有空格,但保留处理)
if (expr[i] == ' ') {
i++;
continue;
}
// 处理数字(可能有多位)
if (isdigit(expr[i])) {
double num = 0;
// 读取整数部分
while (i < n && isdigit(expr[i])) {
num = num * 10 + (expr[i] - '0');
i++;
}
// 如果有小数点,读取小数部分
if (i < n && expr[i] == '.') {
i++;
double fraction = 0.1;
while (i < n && isdigit(expr[i])) {
num += (expr[i] - '0') * fraction;
fraction *= 0.1;
i++;
}
}
vals.push(num);
}
// 处理变量(单个小写字母)
else if (isalpha(expr[i])) {
string var(1, expr[i]); // 转换为字符串
if (variables.find(var) != variables.end()) {
vals.push(variables[var]);
} else {
// 题目保证变量已定义,这里为了安全处理
vals.push(0);
}
i++;
}
// 处理左括号
else if (expr[i] == '(') {
ops.push(expr[i]);
i++;
}
// 处理右括号
else if (expr[i] == ')') {
while (!ops.empty() && ops.top() != '(') {
double val2 = vals.top(); vals.pop();
double val1 = vals.top(); vals.pop();
char op = ops.top(); ops.pop();
vals.push(applyOp(val1, val2, op));
}
ops.pop(); // 弹出左括号
i++;
}
// 处理运算符
else if (isOperator(expr[i])) {
// 处理负号的情况(表达式开头或前面是左括号)
if (expr[i] == '-' && (i == 0 || expr[i-1] == '(' || isOperator(expr[i-1]))) {
// 处理负数:压入0,然后处理减号
vals.push(0);
}
while (!ops.empty() && precedence(ops.top()) >= precedence(expr[i])) {
double val2 = vals.top(); vals.pop();
double val1 = vals.top(); vals.pop();
char op = ops.top(); ops.pop();
vals.push(applyOp(val1, val2, op));
}
ops.push(expr[i]);
i++;
}
else {
i++; // 跳过其他字符
}
}
// 处理栈中剩余的运算符
while (!ops.empty()) {
double val2 = vals.top(); vals.pop();
double val1 = vals.top(); vals.pop();
char op = ops.top(); ops.pop();
vals.push(applyOp(val1, val2, op));
}
return vals.top();
}
};
int main() {
map<string, double> variables; // 存储变量名和值
ExpressionEvaluator evaluator(variables); // 表达式求值器
string line;
// 设置输出格式为保留两位小数
cout << fixed << setprecision(2);
while (getline(cin, line)) {
if (line.empty()) continue; // 跳过空行
// 检查是否为赋值语句(包含等号)
size_t eq_pos = line.find('=');
if (eq_pos != string::npos) {
// 赋值语句处理
string var_name = line.substr(0, eq_pos);
string expr = line.substr(eq_pos + 1);
// 计算表达式值
double result = evaluator.evaluate(expr);
variables[var_name] = result;
}
else {
// 使用字符串流解析其他语句
stringstream ss(line);
string command;
ss >> command;
if (command == "exit") {
break; // 结束程序
}
else if (command == "read") {
// 读取变量名列表
vector<string> vars_to_read;
string var;
while (ss >> var) {
vars_to_read.push_back(var);
}
// 读取下一行的值
if (getline(cin, line)) {
stringstream ss_values(line);
double value;
for (size_t i = 0; i < vars_to_read.size(); i++) {
if (ss_values >> value) {
variables[vars_to_read[i]] = value;
}
}
}
}
else if (command == "print") {
// 读取要输出的变量名
vector<string> vars_to_print;
string var;
while (ss >> var) {
vars_to_print.push_back(var);
}
// 输出变量值
for (size_t i = 0; i < vars_to_print.size(); i++) {
if (variables.find(vars_to_print[i]) != variables.end()) {
cout << variables[vars_to_print[i]];
} else {
cout << "0.00"; // 未定义变量输出0.00
}
if (i != vars_to_print.size() - 1) {
cout << " ";
}
}
cout << endl;
}
}
}
return 0;
}
DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。
更多推荐


所有评论(0)