北京理工大学复试上机--2012
Posted ache
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了北京理工大学复试上机--2012相关的知识,希望对你有一定的参考价值。
1、输入十个正整数数字从小到大排序
输入:1 2 5 7 9 10 45 67 24 26
输出:1,2,5,7,9,10,24,26,45,67
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int a, i; vector<int> v; for (i = 0; i < 10; i++) { scanf("%d", &a); v.push_back(a); } sort(v.begin(), v.end()); for (i = 0; i < 10; i++) { cout << v[i]; if (i < 9) 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 <iostream> #include <vector> #include <algorithm> using namespace std; struct student { int age, id; string name, gender; }; bool cmp(student t1, student t2) { if (t1.id != t2.id) return t1.id > t2.id; } int main() { int a, i, j; string s; student stu; vector<student> v; student st[3]; st[0].id = 10; st[0].name = "wes"; st[0].gender = "f"; st[0].age = 23; st[1].id = 20; st[1].name = "erf"; st[1].gender = "f"; st[1].age = 45; st[2].id = 30; st[2].name = "str"; st[2].gender = "t"; st[2].age = 89; v.push_back(st[0]); v.push_back(st[1]); v.push_back(st[2]); while (getline(cin, s)) { string str[4]; if (s[0] == ‘I‘) { int n = (s[1] - ‘0‘) * 10 + (s[2] - ‘0‘); stu.id = n; n = s[s.length() - 1] - ‘0‘ + (s[s.length() - 2] - ‘0‘) * 10; stu.age = n; string ss = ""; for (i = 4; i < s.length(); i++) { if (s[i] == ‘,‘) { stu.name = ss; ss = ""; break; } ss += s[i]; } for (j = i + 1; j < s.length(); j++) { if (s[j] == ‘,‘) { stu.gender = ss; break; } ss += s[j]; } v.push_back(stu); sort(v.begin(), v.end(), cmp); for (i = 0; i < v.size(); i++) { cout << "(" << v[i].id << "," << v[i].name << "," << v[i].gender << "," << v[i].age << ")"; if (i < v.size() - 1) cout << ","; } } if (s[0] == ‘D‘) { int n = (s[1] - ‘0‘) * 10 + (s[2] - ‘0‘); for (i = 0; i < v.size(); i++) { if (v[i].id == n) { v.erase(v.begin() + i); break; } } sort(v.begin(), v.end(), cmp); for (i = 0; i < v.size(); i++) { cout << "(" << v[i].id << "," << v[i].name << "," << v[i].gender << "," << v[i].age << ")"; if (i < v.size() - 1) cout << ","; } } } return 0; }
3、利用后序和中序确定前序遍历结果
示例:
输入(按后序、中序): CHBEDA CBHADE
输出: ABCHDE
这个题目前还没完全搞清楚,先空着
PS:突入其来的又不会的题,甚是心慌!!! 要踏实一点了,加油!
以上是关于北京理工大学复试上机--2012的主要内容,如果未能解决你的问题,请参考以下文章