修订:
2017保研第二题第(二)问:前缀该为中缀表达式,前缀表达式不可能有括号。
2018考研第三题:输入方式为层次序列顺序。
2018考研第三题,改为 “按输入的学号查找,输出姓名与成绩” 或者 “取消时间复杂度限制”。
2019考研第二题:输入的数据为单调不减,第二问是删除值为mink与maxk之间的结点,不删除mink与maxk结点。(如1->2->2->3->3->4->4,mink=2,maxk=4,则1->2->2->4->4)
2019考研第四题:自身也是自己的祖先。
2019保研第一题:要找的是“完数”,即去掉自身之外包含1的所有因数相加等于本身。(如6 = 1 + 2 + 3)
2019保研第二题:每个数的范围在(-1000 ~ 1000)内(不包括边界)。
2019保研第四题:第(2)问,若出现次数相等,则按单词字母序排序。
//2017考研1
//输出99乘法表
#include <iostream>
using namespace std;
int main()
{
for(int i = 1; i < 10; ++i){
for(int j = 1; j <= i; ++j){
cout << j << "x" << i << "=" << i*j << " ";
}
puts("");
}
return 0;
}
//2017考研2
//求所有具有如下性质的所有四位数:abcd=(ab+cd)^2
#include <iostream>
using namespace std;
int main()
{
for(int ab = 10; ab < 100; ++ab){
for(int cd = 0; cd < 100; ++cd){
if((ab*100 + cd) == (ab + cd) * (ab + cd)) cout << ab*100 + cd << " ";
}
}
return 0;
}
//2017考研3
//打开txt文件,另存为,编码格式该为ANSI
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;
typedef pair<string, int> PSI;
struct Grade{
string id, name, interview, code_grade;
int grade;
};
struct Grade stu[4];
PSI find_name(string name){
for(auto& i:stu){
if(i.name == name) return {i.id, i.grade};
}
return {"no", 0};
}
int main()
{
ifstream t3;
t3.open("D:\\2017k03.txt", ios::in);
int n = 0;
while(!t3.eof()){
t3 >> stu[n].id;
t3 >> stu[n].name;
t3 >> stu[n].interview;
t3 >> stu[n++].code_grade;
}
for(auto& i:stu) i.grade = stoi(i.interview) + stoi(i.code_grade);
sort(stu, stu+4, [&](struct Grade a, struct Grade b){
return a.grade > b.grade;
});
for(auto& i:stu) cout << i.id << " " << i.grade << endl;
string name;
cin >> name;
PSI res = find_name(name);
if(res.first == "no") cout << "查无此人" << endl;
else cout << res.first << " " << res.second << endl;
return 0;
}
//2017考研4
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct abab{
string ab, len;
};
struct abab f[3];
int main()
{
ifstream t4;
t4.open("D:\\t4.txt", ios::in);
int n = 0;
while(!t4.eof()){
t4 >> f[n].ab;
t4 >> f[n++].len;
}
int max_ab = 0;
string res;
for(auto& i:f){
int cnt_a = 0, cnt_b = 0, temp;
for(auto& j:i.ab){
if(j == 'a') cnt_a++;
if(j == 'b') cnt_b++;
}
temp = cnt_a > cnt_b ? cnt_b : cnt_a;
if(temp > max_ab){
max_ab = temp;
res = i.ab;
}
}
cout << res << endl;
return 0;
}
//2017保研1
#include <iostream>
using namespace std;
int main()
{
for(int i = 32; i < 100; ++i){
int num = i * i, idx = 3;
int cnt[4];//存每一位数
while(num){
cnt[idx--] = num % 10;//1234
num /= 10;//123
}
if(cnt[0] == cnt[1] && cnt[2] == cnt[3] && cnt[0] != cnt[3])
cout << i * i << endl;
}
return 0;
}
//2017保研2
#include <iostream>
#include <string>
#include <stack>
using namespace std;
bool check_mid(string str){
stack<char> s;
for(int i = 0; i < str.length(); ++i){
if(str[i] == '(') s.push(str[i]);
else if(str[i] == ')'){
if(s.top() != '(') return 0;
s.pop();//((()
}else if(str[i] == '#'){
if(!s.empty()) return 0;
}
}
return 1;
}
int cal(int a, char op, int b){
switch(op){
case '+': return a + b;
case '-': return a - b;
case '*': return a * b;
case '/': return a / b;
}
return 0;
}
int judge(char t){
if(t == '+' || t == '-') return 1;
else if(t == '*' || t == '/') return 2;
else return 0;
}
int cal_mid(string str){
/*
出现数字,直接放进数字栈
出现左括号 直接放进符号栈
出现右括号 while !=(
出现# while 栈不空
*/
stack<char> op_fu;
stack<int> op_num;
op_fu.push('#');
int a, b;
char op;
for(int i = 0; i < str.length(); ++i){
if(str[i] >= '0' && str[i] <= '9') op_num.push(str[i] - '0');
else if(str[i] == '(') op_fu.push(str[i]);
else if(str[i] == ')'){
while(op_fu.top() != '('){ //a在b前面+
b = op_num.top();
op_num.pop();
a = op_num.top();
op_num.pop();
op = op_fu.top();
op_fu.pop();
op_num.push(cal(a, op, b));
}
op_fu.pop();
}else if(str[i] == '#'){
while(op_fu.top() != '#'){ //a在b前面+
b = op_num.top();
op_num.pop();
a = op_num.top();
op_num.pop();
op = op_fu.top();
op_fu.pop();
op_num.push(cal(a, op, b));
}
break;
}else{//此时是运算符
if(judge(op_fu.top()) < judge(str[i])) op_fu.push(str[i]);
else{
while(judge(op_fu.top()) >= judge(str[i]) && op_fu.top() != '#'){
b = op_num.top();
op_num.pop();
a = op_num.top();
op_num.pop();
op = op_fu.top();
op_fu.pop();
op_num.push(cal(a, op, b));
}
op_fu.push(str[i]);
}
}
}
return op_num.top();
}
int main()
{
string mid_s;
cin >> mid_s;
if(check_mid(mid_s)) cout << "first input expression is True" << endl;
else cout << "first input expression is False" << endl;
cin >> mid_s;
if(check_mid(mid_s)) {
cout << "second input expression is True" << endl;
cout << cal_mid(mid_s) << endl;
}
else cout << "second input expression is False" << endl;
return 0;
}
/*
如输入3*(5-8/2)+7,输出是10;
2+3*(7-4)+8/4 -> 13;
*/
//2017保研3
#include <iostream>
#include <string>
#include <fstream>
#include <algorithm>
using namespace std;
struct Grade{
string id, jishi, siji;
double zonghe;
int del;
};
int main()
{
ifstream t3;
int n = 0;
t3.open("D:\\t3.txt", ios::in);
struct Grade stu[3];
while(!t3.eof()){
stu[n].del = 0;
t3 >> stu[n].id;
t3 >> stu[n].jishi;
t3 >> stu[n++].siji;
}
for(int i = 0; i < 3; ++i){
stu[i].zonghe = 0.85 * stod(stu[i].jishi) + 0.15 * stod(stu[i].siji);
if(stoi(stu[i].siji) < 425) stu[i].del = 1;
}
sort(stu, stu+3, [&](struct Grade a, struct Grade b){
return a.zonghe > b.zonghe;
});
for(auto& i:stu) cout << i.id << " " << i.zonghe << " " << i.siji << endl;
puts("--------------------");
for(auto& i:stu) if(!i.del) cout << i.id << " " << i.zonghe << " " << i.siji << endl;
return 0;
}
//2017保研4
#include <iostream>
using namespace std;
int n, m, max_num = -10000;
bool judge_zj(int *a, int *b){
int flg;
for(int i = 0; i < n; ++i){
flg = 0;
for(int j = 0; j < m; ++j){
if(a[i] == b[j]) flg++;
}
if(!flg) return 0;
}
return true;
}
bool judge_sq(int *a, int *b){
for(int i = 0, j = 0; j < m;){
if(a[i] == b[j]){
++i, ++j;
}else{
j = j - i + 1;
i = 0;
}
if(i > n - 1) return 1;
}
return 0;
}
int main()
{
cin >> n;
int a[n];
for(int i = 0; i < n; ++i){
cin >> a[i];
max_num = max(max_num, a[i]);
}
cin >> m;
int b[m];
for(int i = 0; i < m; ++i){
cin >> b[i];
max_num = max_num > b[i] ? max_num : b[i];
}
cout << "Max num is:" << max_num << endl;
if(judge_sq(a, b)) cout << "a是b的子序列" << endl;
else cout << "no zxl" << endl;
if(judge_zj(a, b)) cout << "a是b的子ji" << endl;
else cout << "no ji" << endl;
return 0;
}
// a 1 2 3
// b 0 5 1 3 1 2 3 5 6
//2018考研1
#include <iostream>
using namespace std;
struct ListNode{
int val;
ListNode *next;
};
void pr(ListNode *p){
while(p){
cout << p->val << " ";
p = p->next;
}
puts("");
}
int main() {
int n;
cin >> n;
ListNode *head = new ListNode(), *p;
head->val = -1;
while(n--){
int temp;
cin >> temp;
p = new ListNode();
p->val = temp;
p->next = head->next;
head->next = p;
}
pr(head->next);
return 0;
}
//2018考研2
#include <iostream>
using namespace std;
int main() {
int a[10000];
int cnt = 0;
do{
cin >> a[cnt++];
}while(cin.get() != '\n');
int s, p = -1, q = -1;
cin >> s;
for(int i = 0; i < cnt; ++i){
if(a[i] == s){
p = i;
break;
}
}
for(int i = cnt - 1; i > 0; --i){
if(a[i] == s){
q = i;
break;
}
}
cout << p << " " << q << endl;
return 0;
}
//2018考研3
#include <iostream>
using namespace std;
struct TNode{
int data;
TNode *lchild, *rchild;
};
TNode* creatT(int *node, int n){
TNode *tp[n];
for(int i = 0; i < n; ++i){
tp[i] = new TNode();
tp[i]->data = node[i];
tp[i]->lchild = NULL;
tp[i]->rchild = NULL;
}
for(int i = 0; i <= n/2-1; ++i){
if(2*i+1 < n) tp[i]->lchild = tp[2*i+1];
if(2*i+2 < n) tp[i]->rchild = tp[2*i+2];
}
return tp[0];
}
void print(TNode *p){
if(p){
cout << p->data << " ";
print(p->lchild);
print(p->rchild);
}
}
int res = -100000;
int get_max(TNode *p){
if(!p) return 0;
int left = max(0, get_max(p->lchild));
int right = max(0, get_max(p->rchild));
res = max(res, p->data + left + right);
return max(left, right) + p->data;
}
int main() {
int node[10000];
int cnt = 0;
TNode *root;
do{
cin >> node[cnt++];
}while(cin.get() != '\n');
root = creatT(node, cnt);
print(root);
get_max(root);
cout << res <<endl;
return 0;
}
//2018考研4
#include <iostream>
#include <vector>
using namespace std;
vector<vector<int>> setsa(int *nums, int n){
vector<vector<int>> res;
res.push_back({});
for(int i = 0; i < n; ++i){
res.push_back({nums[i]});
int temp = res.size() - 1;
for(int k = 1; k < temp; ++k){
res.push_back(res[k]);//复制
res[res.size()-1].push_back(nums[i]);//加上刚放进res中的那个元素
}
}
return res;
}
int main() {
int a[10000];
int cnt = 0;
do{
cin >> a[cnt++];
}while(cin.get() != '\n');
vector<vector<int>> res = setsa(a, cnt);
for(auto& i:res){
for(auto& j:i){
cout << j << " ";
}
puts("");
}
return 0;
}
//2018保研1
/*
1.求100到999之间的水仙花数。
水仙花数是指一个 3 位数,它的每个位上的数字的 3次幂之和等于它本身
(例如:1^3 + 5^3+ 3^3 = 153)
153 370 371 407
pow(a, 3); == a^3;
*/
#include <iostream>
using namespace std;
int pow3(int a){
return a*a*a;
}
int main() {
for(int i = 100; i < 1000; ++i){
int cnt[3];
int temp = i, poi = 2;
while(temp){
cnt[poi--] = temp % 10;
temp /= 10;
}
if(pow3(cnt[0]) + pow3(cnt[1]) + pow3(cnt[2]) == i)
cout << i << " ";
}
return 0;
}
//2018保研2
/*
2. 输入10个整数,完成以下操作: 求出10个整数中最大数并输出;
将10个整数逆序存放(要倒过来存在数组中)并输出,
上述两个算法时间复杂度为O(n)且空间复杂度为O(1)。
*/
#include <iostream>
using namespace std;
int main() {
int a[10];
int max_num = -10000;
for(int i = 0; i < 10; ++i){
cin >> a[i];
if(a[i] > max_num) max_num = a[i];
}
cout << max_num << endl;
for(int i = 0; i < 5; ++i){
a[i] = a[i] ^ a[9-i];
a[9-i] = a[i] ^ a[9-i];
a[i] = a[i] ^ a[9-i];
}
for(int i:a) cout << i << " ";
return 0;
}
//2018保研3
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<string, double> PSD;
struct Stu{
int id;
double grade;
string name;
};
struct TNode{
struct Stu s;
TNode *lchild, *rchild;
TNode(struct Stu x): s(x), lchild(NULL), rchild(NULL){}
};
TNode* insertT(TNode *root, struct Stu x){
TNode *p = new TNode(x);
if(!root) root = p;
else if(x.id > root->s.id) root->lchild = insertT(root->lchild, x);
else if(x.id < root->s.id) root->rchild = insertT(root->rchild, x);
return root;
}
TNode* creatBST(struct Stu x[]){
TNode *p = new TNode(x[0]);
for(int i = 1; i < 5; ++i){
insertT(p, x[i]);
}
return p;
}
PSD find_id(int id, TNode *root){
if(id == root->s.id) return {root->s.name, root->s.grade};
else if(id < root->s.id && root->lchild) return find_id(id, root->lchild);
else if(id > root->s.id && root->rchild) return find_id(id, root->rchild);
else return {"None", 0};
}
int main() {
struct Stu stu[5];
for(int i = 0; i < 5; ++i){
cin >> stu[i].id >> stu[i].name >> stu[i].grade;
}
sort(stu, stu+5, [&](struct Stu a, struct Stu b){
return a.grade < b.grade;
});
puts("------------");
for(auto i:stu) cout << i.id << " " << i.name << " " << i.grade << endl;
TNode *root = creatBST(stu);
int cinid;
cin >> cinid;
PSD res = find_id(cinid, root);
if(res.first == "None") cout << "None" <<endl;
else cout << res.first << " " << res.second << endl;
return 0;
}
/*
1001 张三 100
1002 李四 95
1003 王五 40
1004 特朗普 80
1005 六月 56
*/
//2018保研4
/*
A(B(,G(,)),C(D(F(,),),E(,)))。
*/
#include <iostream>
#include <string>
#include <stack>
using namespace std;
struct TNode{
char data;
TNode *lchild, *rchild;
};
TNode* str2BTree(string str, TNode* &T){
stack<TNode*> st;
TNode *p;
int flag = -1;
if(T) {
delete(T);
T = nullptr;
}
for(int i = 0; i < str.length(); ++i){
switch(str[i]){//A(B(,G(,)),C(D(F(,),),E(,)))
case '(':
if(p){
flag = 0;//左孩子
st.push(p);
p = NULL;
}
break;
case ')'://A(,)
st.pop();
break;
case ',':
flag = 1;//右孩子
break;
default:
p = new TNode();
p->data = str[i];
if(!T) T = p;
else{
if(flag) st.top()->rchild = p;
else st.top()->lchild = p;
}
break;
}
}
return T;
}
void pr(TNode *t){
if(t){
pr(t->lchild);
cout << t->data << " ";
pr(t->rchild);
}
}
int main() {
string str;
cin >> str;
TNode *root = new TNode();
root = str2BTree(str, root);
pr(root);
return 0;
}
//2019考研1
/*
1.输入长度为10的数组,并完成下列操作:
(1)输出其中的最大值;
(2)将数组逆序并输出;
*/
#include <iostream>
using namespace std;
int main(){
int a[10];
int max_num = 0;
for(int i = 0; i < 10; ++i){
cin >> a[i];
max_num = max(max_num, a[i]);
}
cout << max_num <<endl;
for(int i = 0; i < 5; ++i){
a[i] = a[i] ^ a[9-i];
a[9-i] = a[i] ^ a[9-i];
a[i] = a[i] ^ a[9-i];
}
for(int i = 0; i < 10; ++i) cout << a[i] << " ";
return 0;
}
//2019考研2
#include <iostream>
using namespace std;
struct ListNode{
int val;
ListNode *next;
};
void del_node(ListNode *p, int mink, int maxk){
//1 2 2 3 4 4 5 mink = 2, maxk = 4 1 2 2 4 4 5
while(p->val != mink || p->next->val == mink) p = p->next;
while(p->next->val != maxk) p->next = p->next->next;
}
void print(ListNode *p){
while(p){
cout << p->val << " ";
p = p->next;
}
puts("");
}
int main(){
ListNode *head = new ListNode(), *p, *q;
head->val = -1;
p = head;
int n;
cin >> n;
while(n--){
q = new ListNode();
cin >> q->val;
p->next = q;
p = q;
}
print(head->next);
int mink, maxk;
cin >> mink >> maxk;
del_node(head, mink, maxk);
print(head->next);
return 0;
}
//2019考研3
#include <iostream>
#include <string>
using namespace std;
bool check_s_num(string s){
int cnt = 1, temp = 0;
for(int i = 1; i < s.length(); ++i){
if(s[i] == s[i-1]) cnt++;
else if(temp == 0){
temp = cnt;
cnt = 1;
continue;
}else{
if(cnt != temp) return 0;
cnt = 1;
}
}
return 1;
}
int main(){
string s;
cin >> s;
if(check_s_num(s)) cout << "Yes" << endl;
else cout << "No" << endl;
return 0;
}
//2019考研4
#include <iostream>
using namespace std;
struct TNode{
char data;
TNode *lchild, *rchild;
};
void creatT(TNode* &p){
char s;
cin >> s;
if(s == '#') p = NULL;
else{
p = new TNode();
p->data = s;
creatT(p->lchild);
creatT(p->rchild);
}
}
TNode* com_ancestor(TNode *root, char a, char b){
if(!root || root->data == a || root->data == b) return root;
TNode *left = com_ancestor(root->lchild, a, b);
TNode *right = com_ancestor(root->rchild, a, b);
if(left && right) return root;
return left == NULL ? right : left;
}
void print(TNode *p){
if(p){
print(p->lchild);
cout << p->data << " ";
print(p->rchild);
}
}
int main(){
TNode *root, *ancestor;
creatT(root);
print(root);
puts("");
char a, b;
cin >> a >> b;
ancestor = com_ancestor(root, a, b);
cout << ancestor->data << endl;
return 0;
}
//356##27##4##10##8##
//2019保研1
/*
要找的是“完数”,即去掉自身之外包含1的所有因数相加等于本身。
(如6 = 1 + 2 + 3)
*/
#include <iostream>
using namespace std;
int main(){
for(int i = 2; i < 1000; ++i){
int k = 0;
for(int j = 1; j < i/2+1; ++j){
if(i % j == 0) k += j;
}
if(k == i) cout << i << " ";
}
return 0;
}
//2019保研2
#include <iostream>
#include <cstring>
using namespace std;
struct ListNode{
int val;
ListNode *next;
};
void del_same(ListNode *head){
if(!head->next) return;
int cnt[1000];
ListNode *p = head->next, *q = head;
memset(cnt, 0, sizeof(cnt));
while(p){
cnt[abs(p->val)]++;
if(cnt[abs(p->val)] > 1){
q->next = p->next;
p = q->next;
continue;
}
p = p->next;
q = q->next;
}
}
void print(ListNode *p){
while(p){
cout << p->val << " ";
p = p->next;
}
puts("");
}
int main(){
int n, z_cnt = 0;
cin >> n;
ListNode *head = new ListNode(), *p, *q;
head->val = -1;
p = head;
while(n--){
q = new ListNode();
cin >> q->val;
if(q->val > 0) z_cnt++;
p->next = q;
p = q;
}
cout << z_cnt << endl;
print(head->next);
del_same(head);
print(head->next);
return 0;
}
//2019保研3
#include <iostream>
using namespace std;
struct TNode{
char data;
TNode *lchild, *rchild;
};
TNode* insert_node(TNode *root, char x){
TNode *p = new TNode();
p->data = x;
if(!root) root = p;
else if(x < root->data) root->lchild = insert_node(root->lchild, x);
else if(x > root->data) root->rchild = insert_node(root->rchild, x);
return root;
}
TNode* creatBST(char *x, int n){
TNode *p = new TNode();
p->data = x[0];
for(int i = 1; i < n; ++i){
insert_node(p, x[i]);
}
return p;
}
void mid_print(TNode *p){
if(p){
mid_print(p->lchild);
cout << p->data << " ";
mid_print(p->rchild);
}
}
int max_deep(TNode *p){
if(!p) return 0;
else return max(max_deep(p->lchild), max_deep(p->rchild)) + 1;
}
int min_deep(TNode *p){
if(!p) return 0;
else return min(min_deep(p->lchild), min_deep(p->rchild)) + 1;
}
bool is_com_tree(TNode *root){
if(!root) return 1;
bool flag = 0;
TNode* q[10000];
int first = 0, last = 0;
q[last++] = root;
while(last - first){
TNode *tmp = q[first++];
if(!tmp) flag = 1;
else{
if(flag) return 0;
q[last++] = tmp->lchild;
q[last++] = tmp->rchild;
}
}
return 1;
}
int main(){
int n;
cin >> n;
char node[n];
for(int i = 0; i < n; ++i) cin >> node[i];
TNode *root = creatBST(node, n);
mid_print(root);
puts("");
cout << "Max deep is:" << max_deep(root) << endl;
cout << "Min deep is:" << min_deep(root) << endl;
if(is_com_tree(root)) cout << "Yes" << endl;
else cout << "No" << endl;
return 0;
}
//2019保研4
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct ListNode{
string word;
int cnt;
ListNode *next;
int flag;
ListNode(string x): word(x), cnt(1), flag(0), next(NULL){}
};
void print(ListNode *p){
while(p){
cout << p->word << p->cnt << "->";
p = p->next;
}
puts("");
}
int find_word(ListNode *p, string word){
while(p){
if(p->word == word) return p->cnt;
p = p->next;
}
return 0;
}
void print_ks_words(ListNode *head, int k){
for(int i = 0; i < k; ++i){
int max_num = 0;
ListNode *p = head, *q;
while(p){
if(!p->flag && p->cnt > max_num){
q = p;
max_num = p->cnt;
}
p = p->next;
}
q->flag = 1;
cout << q->word << " ";
}
}
int main(){
ifstream t4;
t4.open("D:\\2019b04.txt", ios::in);
int word_num;
t4 >> word_num;
ListNode *head = new ListNode("HEAD_NODE"), *p, *q, *t;
head->cnt = -1;
p = head;
for(int i = 0; i < word_num; ++i){
string temp;
t4 >> temp;
t = head;
while(t){
if(t->word == temp){
t->cnt++;
break;
}
t = t->next;
}
if(t) continue;
else{
q = new ListNode(temp);
p->next = q;
p = q;
}
}
print(head->next);
string find_s;
cin >> find_s;
int res = find_word(head, find_s);
if(!res) cout << "None" <<endl;
else cout << res << endl;
int k;
cin >> k;
print_ks_words(head, k);
return 0;
}