北理工计算机考研复试上机2012年真题
1、输入十个正整数数字从小到大排序
输入:1 2 5 7 9 10 45 67 24 26
输出:1,2,5,7,9,10,24,26,45,67
代码:
#include <bits/stdc++.h>
using namespace std;
vector<int> a;
vector<int> tmp(100);
void merge_sort(int l, int r) {
if (l >= r) return;
int mid = l + r >> 1;
merge_sort(l, mid);
merge_sort(mid+1, r);
int k = 0, i = l, j = mid+1;
while (i <= mid && j <= r) {
if (a[i] < a[j]) tmp[k++] = a[i++];
else tmp[k++] = a[j++];
}
while (i <= mid) tmp[k++] = a[i++];
while (j <= r) tmp[k++] = a[j++];
for (int i = l, k = 0; i <= r; i++, k++) a[i] = tmp[k];
}
void quick_sort(int l, int r) {
if (l >= r) return;
int mid = l + r >> 1, i = l-1, j = r+1;
while (i < j) {
do i++; while (a[i] < a[mid]);
do j--; while (a[j] > a[mid]);
if (i < j) swap(a[i], a[j]);
}
quick_sort(l, j);
quick_sort(j+1,r);
}
int main()
{
for (int i = 0; i < 10; i++) {
int num;
scanf("%d,",&num);
a.push_back(num);
}
merge_sort(0, 9);
for (auto it = a.begin(); it != a.end(); it++) {
cout << *it;
if (it != a.end() - 1) cout << ',';
}
return 0;
}
2、学生有(学号,姓名,性别,年龄),初始化三个学生的信息
(10, wes, f, 23) (20, ert, f, 45) (30, str, t, 89) ,然后对学生信息进行插入和删
除处理
例如: I12,rt, f, 67表示插入12,rt, f, 67
D10表示删除学号为10的学生的信息
每次操作完成以后输出所有学生的信息按学号从大到小排序
输入: I12,rt,f,67
输出: (30,str,t,89),(20,erf,f,45),(12,rt,f,67),(10,wes,f,23)
输入: D10
输出: (30,str,t,89),(20,erf,f,45),(12,rt,f,67)
代码:
#include <bits/stdc++.h>
using namespace std;
struct student {
string sno;
string name;
string gender;
int age;
};
vector<student> stu;
bool del(string sno) {
for (auto it = stu.begin(); it != stu.end(); it++) {
if (it->sno == sno) {
stu.erase(it);
return true;
}
}
return false;
}
bool cmp(student s1, student s2) {
return s1.sno > s2.sno;
}
int main()
{
stu.push_back({"10","wes","f",23});
stu.push_back({"20","ert","f",45});
stu.push_back({"30","str","t",89});
string s;
while (1) {
cout << "输入:";
getline(cin, s);
if (s == "#") break;
if (s[0] == 'I') {
s = s.substr(1);
// sno
int idx = s.find(',');
string sno = s.substr(0, idx);
s = s.substr(idx+1);
// name
idx = s.find(',');
string name = s.substr(0, idx);
s = s.substr(idx+1);
// gender
idx = s.find(',');
string gender = s.substr(0,idx);
s = s.substr(idx+1);
// age
int age = stoi(s);
stu.push_back({sno, name, gender, age});
} else if (s[0] == 'D') {
s = s.substr(1);
if (!del(s)) {
cout << "无此人信息,无法删除。" << endl;
}
} else cout << "输入有误。" << endl;
// 输出
sort(stu.begin(), stu.end(), cmp);
cout << "输出:";
for (auto it = stu.begin(); it != stu.end(); it++) {
cout << '(' << it->sno << ',' << it->name <<','
<< it->gender <<','<< it->age << ')';
if (it+1 != stu.end()) cout << ',';
}
cout << endl;
}
return 0;
}
3、利用后序和中序确定前序遍历结果
示例:
输入(按后序、中序): CHBEDA CBHADE
输出: ABCHDE
注:推荐一篇力扣题解,非常棒。力扣题解
代码:
#include<bits/stdc++.h>
using namespace std;
map<char,int> m;
string pre, in, post;
struct Treenode{
char val;
Treenode* left;
Treenode* right;
Treenode(){}
Treenode(char val):val(val),left(nullptr), right(nullptr)
{}
};
Treenode* preAndIn(int ps, int pe, int is, int ie, string pre) {
if (ps > pe || is > ie) return nullptr;
Treenode* cur = new Treenode(pre[ps]);
int ri = m[pre[ps]];
cur->left = preAndIn(ps+1, ps+ri-is, is, ri-1, pre);
cur->right = preAndIn(ps+ri-is+1, pe, ri+1, ie, pre);
return cur;
}
void postcout(Treenode* root) {
if (root == nullptr) return ;
postcout(root->left);
postcout(root->right);
cout << root->val;
}
Treenode* postAndIn(int is, int ie, int ps, int pe, string post) {
if (is > ie || ps > pe) return nullptr;
Treenode* cur = new Treenode(post[pe]);
int ri = m[post[pe]];
cur->left = postAndIn(is, ri-1, ps, ps+ri-is-1, post);
cur->right = postAndIn(ri+1, ie, ri+ps-is, pe-1, post);
return cur;
}
void precout(Treenode* root) {
if (root == nullptr) return ;
cout << root->val;
precout(root->left);
precout(root->right);
}
int main()
{
// 1、输入前序中序,给出后序
// cout << "请输入前序:";
// cin >> pre;
// cout << "请输入中序:";
// cin >> in;
// for (int i = 0; i < in.size(); i++) m[in[i]] = i;
// Treenode* root = preAndIn(0, in.size()-1, 0, in.size()-1, pre);
// // 遍历
// cout << "后序遍历结果:";
// postcout(root);
// 2、输入中序后序,给出前序
cout << "请输入中序:";
cin >> in;
cout << "请输入后序:";
cin >> post;
for (int i = 0; i < in.size(); i++) m[in[i]] = i;
Treenode* root = postAndIn(0, in.size()-1, 0, in.size()-1, post);
cout << "前序遍历结果:";
precout(root);
}