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

代码仅供参考·2025年7月13日8时30分(Day1)·基础数据结构·C++
最后更新于:2025-07-13 21:33:30
编程的路上没有捷径,但有积累的复利 —— 今天敲下的 100 行,会成为明天解决复杂问题的底气。
Copyright © since 2025 FeatherBlaze (CX2521) All rights reserved.

#A. P33 括弧匹配检验

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

int main() {
char c, s[300], top=0;
while (cin >> c) {
if (c=='(' || c=='[') s[++top] = c;
if (c == ')') {
if (s[top] == '(') top--;
else {
cout << "Wrong";
return 0;
}
}
if (c == ']') {
if (s[top] == '[') top--;
else {
cout << "Wrong";
return 0;
}
}
}
if (top > 0) cout << "Wrong";
else cout << "OK";
return 0;
}

#B. P54 后缀表达式的值

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

long long stack[251], x, y;
string s;

int main() {
getline(cin, s, '@');
int len = s.size(), top=0;
for (int i=0; i<len; i++) {
y = stack[top];
if (s[i] == '+') stack[--top] += y;
else if (s[i] == '-') stack[--top] -= y;
else if (s[i] == '*') stack[--top] *= y;
else if (s[i] == '/') stack[--top] /= y;
else if (s[i] == ' ');
else {
x = 0;
while (s[i] != ' ') x = x*10+s[i++]-48;
stack[++top] = x;
}
}
cout << stack[top];
return 0;
}

#C. P41  点赞热度

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

int main() {
int n, k, t, dzs=0, ans=0, q[100001], front=1, rear=0;
cin >> n >> k;
for (int i=1; i<=n; i++) {
cin >> t;
while (t-q[front]>=60 && front<=rear) {
dzs--;
front++;
}
q[++rear] = t;
dzs++;
if (dzs >= k) ans++;
}
cout << ans;
return 0;
}

#D. P42  单词数

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

int main() {
set<string> s;
string str, t;
while (getline(cin, str)) {
for (int i=0; i<str.size(); i++) {
int j=i;
while (str[j]!=' ' && j<str.size()) j++;
t = str.substr(i, j-i);
i = j;
s.insert(t);
}
cout << s.size() << endl;
s.clear();
}
return 0;
}

#E. P43  新生舞会

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

map<string, char> ma;
map<string, char>::iterator it;
int n, m;
string name, id, a, b;
char xb;

int main() {
cin >> n;
while (n--) {
cin >> name >> id >> xb;
ma.insert({name, xb});
ma.insert({id, xb});
}
cin >> m;
while (m--) {
cin >> a >> b;
if (ma[a] != ma[b]) cout << 'Y' << endl;
else cout << 'N' << endl;
}
return 0;
}

#F. P57  [CSP2019 Junior T2] 公交换乘

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

int main(){
int p[100005], t[100005], front=1, rear=0, n, tool, price, time, ans=0, i;
cin >> n;
while (n--) {
cin >> tool >> price >> time;
if(tool == 0) {
rear++;
p[rear] = price;
t[rear] = time;
ans += price;
}
else {
while (front<=rear && time-t[front]>45) front++;
for (i=front; i<=rear; i++)
if(p[i] >= price){
p[i] = 0;
break;
}
if(i>rear)ans+=price;
}
}
cout << ans;
return 0;
}

#G. P64  [NOIP2016普及组T3]海港

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

int n, k, t, gj, tong[100001], ti[300001], na[300005], front=1, rear=0, cnt=0;

int main(){
cin >> n;
while (n--) {
cin >> t >> k;
while (k--) {
cin >> gj;
ti[++rear] = t;
na[rear] = gj;
tong[gj]++;
if (tong[gj] == 1) cnt++;
}
while (t-ti[front] >= 86400) {
tong[na[front]]--;
if (tong[na[front]] == 0) cnt--;
front++;
}
cout << cnt << endl;
}
return 0;
}

评论