Sorry, your browser cannot access this site
This page requires browser support (enable) JavaScript
Learn more >

代码仅供参考·2025年7月17日8时30分(Day5)··C++
最后更新于:2025-07-17 20:25:35
别被 “我学不会” 的念头困住。编程就像拼图,从最小的模块开始,拼着拼着,你就会突然看清整体的轮廓。
Copyright © since 2025 FeatherBlaze (CX2521) All rights reserved.

#A. P108  C班D5T1-最低价成交

Programming… Please wait.

#B. P109  C班D5T2-小美的身高排队大作战

Programming… Please wait.

#C. P110  C班D5T3-维护你的键值对

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <unordered_map>
using namespace std;

int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
unordered_map<int, int> acc;
while (n--) {
char op;
int id, v;
cin >> op >> id;
if (op == 'U') {
cin >> v;
acc[id] = v;
}
else cout << (acc.count(id) ? acc[id] : 100) << endl;
}
return 0;
}

#D. P111  C班D5T4-翻转你的表达式

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
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

string e;
int i = 0;

string a() {
while (i<e.size() && e[i]==' ') i++;
if (i >= e.size()) return "";
int j = i;
while (j<e.size() && e[j]!=' ') j++;
string t = e.substr(i, j-i);
i = j+1;
return t;
}

int b() {
string t = a();
if (t.empty()) return 0;
if (t=="+" || t=="-" || t=="*") {
int l = b();
int r = b();
if (t == "+") return r + l;
if (t == "-") return r - l;
if (t == "*") return r * l;
}
else return stoi(t);
return 0;
}

int main() {
getline(cin, e);
cout << b();
return 0;
}

#E. P122  C班D5T5-树表示的转换

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 <iostream>
#include <algorithm>
using namespace std;

int main() {
int n, child[105][105]={0};
cin >> n;
for (int i=0; i<n-1; i++) {
int u, v;
cin >> u >> v;
int cnt = ++child[v][0];
child[v][cnt] = u;
}
for (int i=1; i<=n; i++) {
int cnt = child[i][0];
for (int j=1; j<=cnt; ++j)
for (int k=j+1; k<=cnt; k++)
if (child[i][j] > child[i][k])
swap(child[i][j], child[i][k]);
}
cout << n << endl;
for (int i=1; i<=n; i++) {
int cnt = child[i][0];
for (int j=1; j<=cnt; j++) cout << child[i][j] << " ";
cout << -1 << endl;
}
return 0;
}

#F. P123  C班D5T6-树表示的转换2

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>
using namespace std;

struct aa {
int id, su;
} a[1000];
int n;

bool cmp(aa a, aa b) {
if (a.su == b.su) {
return a.id < b.id;
}
else {
return a.su < b.su;
}
}

int main() {
cin >> n;
for (int i=1; i<=n; i++) {
while (1) {
int x;
cin >> x;
if (x == -1) break;
a[x].su=i, a[x].id=x;
}
}
sort(a+1, a+1+n, cmp);
cout << n << endl;
for (int i=1; i<=n; i++)
if(a[i].su != 0)
cout << a[i].id << ' ' << a[i].su << endl;
}

#G. P124  C班D5T7-寻找树根

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

int main() {
int n, parent[105]={0};
cin >> n;
for (int i=1; i<=n; i++) {
int child;
while (cin>>child && child!=-1) {
parent[child] = i;
}
}
for (int i=1; i<=n; i++) {
if (parent[i] == 0) {
cout << i;
break;
}
}
return 0;
}

#H. P125  C班D5T8-求后序遍历

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;

void build(int pr[], int ir[], int ps, int pe, int is, int ie) {
if (ps>pe || is>ie) return;
int root=pr[ps], rpi=is;
while (ir[rpi] != root) rpi++;
int lsize = rpi-is;
build(pr, ir, ps+1, ps+lsize, is, rpi-1);
build(pr, ir, ps+lsize+1, pe, rpi+1, ie);
cout << root;
if (ps!=0 || pe!=pe) cout << ' ';
}

int main() {
int n;
cin >> n;
int pr[100], ir[100];
for (int i=0; i<n; i++) cin >> pr[i];
for (int i=0; i<n; i++) cin >> ir[i];
build(pr, ir, 0, n-1, 0, n-1);
return 0;
}

评论