2023 CUMT 中国矿业大学 数据结构课程 课后作业题 代码题解
本博文由我的GitHub仓库LymoneLM/LymoneTest代码自动整理生成,进入仓库可以查看最新的代码
如果代码对您有帮助,希望可以给我的仓库点个Star,或者在GitHub关注我,感谢
在我的个人博客莱蒙黎梦可以查看本博文原文和更多其他我的博文
本博文提供的代码仅供参考学习,原题已遗失,先尝试后使用,不同年度课程题目可能略有差异
A.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| #include <bits/stdc++.h> #pragma GCC optimize(2) #define endl "\n" #define ll long long #define mm(a) memset(a,0,sizeof(a)) using namespace std;
int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; cin>>N; ll studentScore[1010][2]; for(int i=0;i<N;i++) cin>>studentScore[i][0]>>studentScore[i][1]; ll temp; cin>>temp; for(int i=0;i<N;i++) if(studentScore[i][0]==temp){ cout<<studentScore[i][1]; return 0; }
return 0; }
|
B.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| #include <bits/stdc++.h> #pragma GCC optimize(2) #define endl "\n" #define ll long long #define mm(a) memset(a,0,sizeof(a)) using namespace std; int vc[1010][1010]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N,M,temp,location; cin>>N; mm(vc); for(int i=0;i<N;i++){ cin>>temp; vc[i][++vc[i][0]]=temp; } cin>>M; for(int i=0;i<M;i++){ cin>>location>>temp; vc[location-1][++vc[location-1][0]]=temp; } for(int i=0;i<N;i++){ for(int t=vc[i][0];t>=1;t--) cout<<vc[i][t]<<" "; } return 0; }
|
C.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| #include <bits/stdc++.h> #pragma GCC optimize(2) #define endl "\n" #define ll long long #define mm(a) memset(a,0,sizeof(a)) using namespace std;
int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int temp,i=0,data[200]; while(cin>>temp) data[i++]=temp; sort(data,data+i,greater<int>()); while(i--) cout<<data[i]<<" ";
return 0; }
|
D.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| #include <bits/stdc++.h> #pragma GCC optimize(2) #define endl "\n" #define ll long long #define mm(a) memset(a,0,sizeof(a)) using namespace std;
int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int max,temp,cnt=1; cin>>max; while(cin>>temp) if(temp<max) continue; else if(++cnt&&temp>max) max=temp,cnt=1; cout<<cnt;
return 0; }
|
E.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| #include <bits/stdc++.h> #pragma GCC optimize(2) #define endl "\n" #define ll long long #define mm(a) memset(a,0,sizeof(a)) using namespace std;
int main() { ios::sync_with_stdio(false); cin.tie(nullptr); bool child[110]; mm(child); int n,m; cin>>n>>m; int cnt=0,bnt=0; while(cnt!=n) for(int i=0;i<n;i++){ if(!child[i]) bnt++; if(bnt==m){ cout<<i+1<<" "; child[i]=true; cnt++,bnt=0; } }
return 0; }
|
F.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| #include <bits/stdc++.h> #pragma GCC optimize(2) #define endl "\n" #define ll long long #define mm(a) memset(a,0,sizeof(a)) using namespace std;
int main() { ios::sync_with_stdio(false); cin.tie(nullptr); char temp[110]; cin>>temp; int cnt=-1,len=strlen(temp);
stack<char> ch; while(++cnt<len) switch(temp[cnt]){ case '{': ch.push('}'); break; case '[': ch.push(']'); break; case '(': ch.push(')'); break; case ')': case ']': case '}': if(ch.size()==0||ch.top()!=temp[cnt]){ if(ch.size()==0) ch.push(temp[cnt]); goto end; }else ch.pop(); } end:if(ch.size()==0) cout<<"YES"; else switch (ch.top()){ case ')': cout<<"NO1"; break; case ']': cout<<"NO2"; break; case '}': cout<<"NO3"; break; } return 0; }
|
F2.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| #include <iostream> #include <stack> #include <string>
bool checkBrackets(const std::string& expression) { std::stack<char> stack; std::string openingBrackets = "([{"; std::string closingBrackets = ")]}";
for (char c : expression) { if (openingBrackets.find(c) != std::string::npos) { stack.push(c); } else if (closingBrackets.find(c) != std::string::npos) { if (stack.empty() || openingBrackets.find(stack.top()) != closingBrackets.find(c)) { return false; } stack.pop(); } }
return stack.empty(); }
int main() { std::string expression; std::getline(std::cin, expression);
bool bracketsMatch = checkBrackets(expression);
if (bracketsMatch) { std::cout << "YES" << std::endl; } else { std::cout << "NO";
char lastBracket = expression.back(); if (lastBracket == ')') { std::cout << "1"; } else if (lastBracket == ']') { std::cout << "2"; } else if (lastBracket == '}') { std::cout << "3"; }
std::cout << std::endl; }
return 0; }
|
F3.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| #include <iostream> #include <stack> #include <string>
using namespace std;
bool isMatchingPair(char opening, char closing) { if (opening == '(' && closing == ')') return true; if (opening == '[' && closing == ']') return true; if (opening == '{' && closing == '}') return true; return false; }
string checkParentheses(const string& expression) { stack<char> s;
for (char c : expression) { if (c == '(' || c == '[' || c == '{') { s.push(c); } else if (c == ')' || c == ']' || c == '}') { if (s.empty() || !isMatchingPair(s.top(), c)) { if (c == ')') return "NO1"; else if (c == ']') return "NO2"; else if (c == '}') return "NO3"; } else { s.pop(); } } }
return (s.empty()) ? "YES" : "NO3"; }
int main() { string expression; cin >> expression;
cout << checkParentheses(expression) << endl;
return 0; }
|
F4.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| #include <iostream> #include <stack> #include <string>
using namespace std;
bool isMatchingPair(char opening, char closing) { if (opening == '(' && closing == ')') return true; if (opening == '[' && closing == ']') return true; if (opening == '{' && closing == '}') return true; return false; }
string checkParentheses(const string& expression) { stack<char> s;
for (char c : expression) { if (c == '(' || c == '[' || c == '{') { s.push(c); } else if (c == ')' || c == ']' || c == '}') { if (s.empty() || !isMatchingPair(s.top(), c)) { if (c == ')') return "NO1"; else if (c == ']') return "NO2"; else if (c == '}') return "NO3"; } else { s.pop(); } } }
if (!s.empty()) { if (s.top() == '(') return "NO1"; else if (s.top() == '[') return "NO2"; else if (s.top() == '{') return "NO3"; }
return "YES"; }
int main() { string expression; cin >> expression;
cout << checkParentheses(expression) << endl;
return 0; }
|
F5.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| #include <deque> #include <iostream> #include <string> using namespace std; void judge(string s) { deque<char> v; for (int a = 0; a < s.length(); a++) { v.push_back(s[a]); } int a = 0; char c, d; while (a < s.length() / 2) { c = v.front(); v.pop_front(); d = v.back(); v.pop_back(); if (c != '(' && c != '[' && c != '{') { if (d == ')') { cout << "NO1" << endl; return; } else if (d == ']') { cout << "NO2" << endl; return; } else if (d == '}') { cout << "NO3" << endl; return; } else { a++; } } else { if (c == '(' && d != ')') { cout << "NO1" << endl; return; } else if (c == '[' && d != ']') { cout << "NO2" << endl; return; } else if (c == '{' && d != '}') { cout << "NO3" << endl; return; } } } cout << "YES" << endl; return; } int main() { string s; getline(cin, s); judge(s); return 0; }
|
G.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| #include <bits/stdc++.h> #pragma GCC optimize(2) #define endl "\n" #define ll long long #define mm(a) memset(a,0,sizeof(a)) using namespace std;
int main() { ios::sync_with_stdio(false); cin.tie(nullptr); char ch[1010]; cin>>ch; vector<int> num; vector<char> op; int len=strlen(ch); for(int i=0;i<ch;i++){ if(ch[i]>='0'&&ch[i]<='9') }
return 0; }
|
G2.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| #include <iostream> #include <stack> #include <string>
bool isOperator(char c) { return (c == '+' || c == '-' || c == '*' || c == '/'); }
int getPrecedence(char c) { if (c == '+' || c == '-') { return 1; } else if (c == '*' || c == '/') { return 2; } return 0; }
std::string infixToPostfix(const std::string& infix) { std::string postfix; std::stack<char> operators;
for (char c : infix) { if (isOperator(c)) { while (!operators.empty() && operators.top() != '(' && getPrecedence(operators.top()) >= getPrecedence(c)) { postfix += operators.top(); operators.pop(); } operators.push(c); } else if (c == '(') { operators.push(c); } else if (c == ')') { while (!operators.empty() && operators.top() != '(') { postfix += operators.top(); operators.pop(); } if (!operators.empty() && operators.top() == '(') { operators.pop(); } } else { postfix += c; } }
while (!operators.empty()) { postfix += operators.top(); operators.pop(); }
return postfix; }
int main() { std::string infix; std::getline(std::cin, infix);
std::string postfix = infixToPostfix(infix); std::cout << postfix << std::endl;
return 0; }
|
G3.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| #include <iostream> #include <stack> #include <string>
bool isOperator(char c) { return (c == '+' || c == '-' || c == '*' || c == '/'); }
int getPrecedence(char c) { if (c == '+' || c == '-') { return 1; } else if (c == '*' || c == '/') { return 2; } return 0; }
std::string infixToPostfix(const std::string& infix) { std::string postfix; std::stack<char> operators;
for (char c : infix) { if (isOperator(c)) { while (!operators.empty() && operators.top() != '(' && getPrecedence(operators.top()) >= getPrecedence(c)) { postfix += ' '; postfix += operators.top(); operators.pop(); } operators.push(c); } else if (c == '(') { operators.push(c); } else if (c == ')') { while (!operators.empty() && operators.top() != '(') { postfix += ' '; postfix += operators.top(); operators.pop(); } if (!operators.empty() && operators.top() == '(') { operators.pop(); } } else { postfix += c; } }
while (!operators.empty()) { postfix += ' '; postfix += operators.top(); operators.pop(); }
return postfix; }
int main() { std::string infix; std::getline(std::cin, infix);
std::string postfix = infixToPostfix(infix); std::cout << postfix << std::endl;
return 0; }
|
G4.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| #include <iostream> #include <stack> #include <string>
bool isOperator(char c) { return (c == '+' || c == '-' || c == '*' || c == '/'); }
int getPrecedence(char c) { if (c == '+' || c == '-') { return 1; } else if (c == '*' || c == '/') { return 2; } return 0; }
std::string infixToPostfix(const std::string& infix) { std::string postfix; std::stack<char> operators;
for (char c : infix) { if (isOperator(c)) { while (!operators.empty() && operators.top() != '(' && getPrecedence(operators.top()) >= getPrecedence(c)) { postfix += ' '; postfix += operators.top(); operators.pop(); } operators.push(c); } else if (c == '(') { operators.push(c); } else if (c == ')') { while (!operators.empty() && operators.top() != '(') { postfix += ' '; postfix += operators.top(); operators.pop(); } if (!operators.empty() && operators.top() == '(') { operators.pop(); } } else { postfix += ' '; postfix += c; } }
while (!operators.empty()) { postfix += ' '; postfix += operators.top(); operators.pop(); }
return postfix; }
int main() { std::string infix; std::getline(std::cin, infix);
std::string postfix = infixToPostfix(infix); std::cout << postfix << std::endl;
return 0; }
|
G5.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| #include <iostream> #include <stack> #include <string>
bool isOperator(char c) { return (c == '+' || c == '-' || c == '*' || c == '/'); }
int getPrecedence(char c) { if (c == '+' || c == '-') { return 1; } else if (c == '*' || c == '/') { return 2; } return 0; }
std::string infixToPostfix(const std::string& infix) { std::string postfix; std::stack<char> operators;
for (size_t i = 0; i < infix.length(); ++i) { char c = infix[i];
if (std::isdigit(c)) { std::string number; while (i < infix.length() && std::isdigit(infix[i])) { number += infix[i]; ++i; } --i; postfix += number; postfix += ' '; } else if (isOperator(c)) { while (!operators.empty() && operators.top() != '(' && getPrecedence(operators.top()) >= getPrecedence(c)) { postfix += operators.top(); postfix += ' '; operators.pop(); } operators.push(c); } else if (c == '(') { operators.push(c); } else if (c == ')') { while (!operators.empty() && operators.top() != '(') { postfix += operators.top(); postfix += ' '; operators.pop(); } if (!operators.empty() && operators.top() == '(') { operators.pop(); } } }
while (!operators.empty()) { postfix += operators.top(); postfix += ' '; operators.pop(); }
return postfix; }
int main() { std::string infix; std::getline(std::cin, infix);
std::string postfix = infixToPostfix(infix); std::cout << postfix << std::endl;
return 0; }
|
H.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| #include <bits/stdc++.h> #pragma GCC optimize(2) #define endl "\n" #define ll long long #define mm(a) memset(a,0,sizeof(a)) using namespace std;
int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin>>T; cin.get(); char ch[1010]; stack<char> st; while(T--){ cin.getline(ch,1010); int len=strlen(ch); for(int i=0;i<=len;i++){ if(ch[i]==' '||ch[i]=='\0'){ while (st.size()){ cout<<st.top(); st.pop(); } cout<<' '; } else st.push(ch[i]); } cout<<endl; }
return 0; }
|
编程作业、大作业、课程设计程序代码调试、协助/指导,制作软件/脚本/网页,经验丰富质量高
支持C/C++、JAVA、Python、Matlab、JavaScript、R语言等,欢迎咨询
企鹅:3451216814