0%

中国矿业大学数据结构实践4

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
#include <bits/stdc++.h>
using namespace std;
const int N = 10000;
map<string, string> parent;
map<string, int> data;
int main() {
int n;
cin >> n;
for (int i = 0; i < pow(2, n + 1) - 2; i++) {
string m, n;
int len;
cin >> m >> n >> len;
parent[n] = m;
data[n] = len;
}
string p;
cin >> p;
int ans = 0;
while (parent.count(p)) {
ans += data[p];
p = parent[p];
}
cout << ans;
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
32
33
34
35
36
37
38
39
40
41
#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 mapc[27][27][2];
int maxc = 0x3f3f3f3f,wt;

void find(int key, int cost) {
if (key == wt) {
if (cost < maxc)
maxc = cost;
return;
}
for (int i = 1; i <= mapc[key][0][0]; i++)
find(mapc[key][i][0], cost + mapc[key][i][1]);
}

int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);

int N, M;
cin >> N >> M;
mm(mapc);
int pre, next;
int cost;
for (int i = 0; i < M; i++) {
cin >> pre >> next >> cost;
mapc[pre][++mapc[pre][0][0]][0] = next;
mapc[pre][mapc[pre][0][0]][1] = cost;
}
cin>>wt;

find(1, 0);
cout << maxc << endl;

return 0;
}

C.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
#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 mapc[27][27][2];
int maxc = -1;

void find(int key, int cost) {
if (key == 'Z'-'A') {
if (cost > maxc)
maxc = cost;
return;
}
for (int i = 1; i <= mapc[key][0][0]; i++)
find(mapc[key][i][0], cost + mapc[key][i][1]);
}

int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);

int N, M;
cin >> N >> M;
mm(mapc);
char pre, next;
int cost;
for (int i = 0; i < M; i++) {
cin >> pre >> next >> cost;
mapc[pre-'A'][++mapc[pre-'A'][0][0]][0] = next-'A';
mapc[pre-'A'][mapc[pre-'A'][0][0]][1] = cost;
}

find(0, 0);
cout << maxc << endl;

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
23
24
25
26
27
28
29
30
#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;
while (T--) {
int N, temp;
cin >> N;
set<int> st;
for(int i=0;i<N;i++){
cin>>temp;
st.insert(temp);
}
st.erase(st.begin());
if(st.empty())
cout<<"NO"<<endl;
else
cout<<*st.begin()<<endl;
}

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
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;
class num {
public:
long long dat, abs;
friend bool operator<(num A, num B) { return (A.abs == B.abs) ? (A.dat < B.dat) : (A.abs < B.abs); }
void input() {
cin >> dat;
ll a = dat, x = 0;
while (a > 0)
x += a % 10, a /= 10;
abs = x;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);

int n;
cin >> n;
num dat[1010];
for (int i = 0; i < n; i++)
dat[i].input();
sort(dat, dat + n);
while (n--)
cout << dat[n].dat << " \n"[n == 0 ? 1 : 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
#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 x[10], y[10], j = 0, k = 0, temp;
for (int i = 0; i < 10; i++) {
cin >> temp;
if (temp % 2 == 0)
y[k++] = temp;
else
x[j++] = temp;
}
sort(x,x+5,greater<int>());
sort(y,y+5);
for (int i = 0; i < 5; i++)
cout<<x[i]<<" ";
for (int i = 0; i < 5; i++)
cout<<y[i]<<" \n"[i==4?1:0];

return 0;
}

编程作业、大作业、课程设计程序代码调试、协助/指导,制作软件/脚本/网页,经验丰富质量高

支持C/C++、JAVA、Python、Matlab、JavaScript、R语言等,欢迎咨询

企鹅:3451216814

-------------本文结束感谢您的阅读-------------

欢迎关注我的其它发布渠道