提交 719863f3 编写于 作者: 张志晨

add exercises

上级 4ab59f57
# 《教父》家族关系维护
在《教父》中,有五个家族,分别是Corleone家族、Tattaglia家族、Cuneo家族、Stracci家族和Barzini家族。五个家族之间的关系是复杂的,它们之间可能存在敌对关系、盟友关系和中立关系。
你需要写一个程序来计算五个家族之间的关系。你需要输入五个家族之间的关系,然后输出五个家族之间的关系矩阵。
你需要自定义一个算法来计算五个家族之间的关系,算法的输入是五个家族之间的关系,输出是五个家族之间的关系矩阵。
算法的实现方式是你自己决定的,但你需要考虑这道题的时间复杂度和空间复杂度。
## 输入格式
输入第一行包含五个整数,分别表示Corleone家族、Tattaglia家族、Cuneo家族、Stracci家族和Barzini家族之间的关系。整数的取值范围是-1、0、1,分别表示敌对关系、中立关系和盟友关系。
## 输出格式
输出五行,每行五个整数,分别表示Corleone家族、Tattaglia家族、Cuneo家族、Stracci家族和Barzini家族之间的关系。整数的取值范围是-1、0、1,分别表示敌对关系、中立关系和盟友关系。
## 输入样例
1 -1 0 0 -1
## 输出样例
1 -1 0 0 -1
-1 1 0 0 1
0 0 1 0 0
0 0 0 1 0
-1 1 0 0 1
## 提示
\ No newline at end of file
#include <iostream>
#include <sstream>
using namespace std;
const int N = 5;
int a[N][N];
int main() {
string line;
getline(cin, line);
stringstream ss(line);
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (i == j) {
a[i][j] = 1; // 同家族之间是盟友关系
} else {
a[i][j] = 0; // 默认是中立关系
}
}
}
// 从输入中读入五个家族之间的关系
for (int i = 0; i < N; i++) {
ss >> a[i][i];
}
// 输出五个家族之间的关系矩阵
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cout << a[i][j] << ' ';
}
cout << endl;
}
return 0;
}
1 -1 0 0 -1
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
1 0 0 0 1
\ No newline at end of file
1 -1 1 -1 1
-1 1 -1 1 -1
1 -1 1 -1 1
-1 1 -1 1 -1
1 -1 1 -1 1
\ No newline at end of file
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
\ No newline at end of file
1 -1 -1 -1 -1
-1 1 -1 -1 -1
-1 -1 1 -1 -1
-1 -1 -1 1 -1
-1 -1 -1 -1 1
\ No newline at end of file
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
\ No newline at end of file
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
\ No newline at end of file
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
\ No newline at end of file
1 -1 1 -1 1
-1 1 -1 1 -1
1 -1 1 -1 1
-1 1 -1 1 -1
1 -1 1 -1 1
\ No newline at end of file
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
\ No newline at end of file
1 -1 -1 -1 -1
-1 1 -1 -1 -1
-1 -1 1 -1 -1
-1 -1 -1 1 -1
-1 -1 -1 -1 1
\ No newline at end of file
# 寻找宝藏山
一天,你去了一个神秘的森林,在那里你遇到了一个神秘的老人。他告诉你,森林里有一座宝藏山,但是要想到达宝藏山,你必须先通过一些森林的入口。
这个老人给你了一张地图,地图上有若干个入口和宝藏山,每个入口都有一个花费。你可以从任意一个入口开始,然后经过一些入口,最终到达宝藏山。但是你有一个限制,你只能走 $K$ 步,如果走的步数超过了 $K$,那么你就无法到达宝藏山,也就无法获得宝藏。
你必须实现一个程序,接受用户输入的地图信息,并计算出,你能够到达宝藏山的最小花费。
## 输入描述
第一行包含三个整数 $N$、$M$ 和 $K$,$N$ 表示入口的个数、$M$ 表示边的个数、$K$ 表示最多走的步数。
接下来 $M$ 行,每行包含三个整数 $A$、$B$ 和 $C$,表示一条从 $A$ 到 $B$ 的有向边,边权为 $C$。
## 输出描述
输出一行,包含一个整数,表示你能够到达宝藏山的最小花费。
## 输入样例
5 10 6
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
5 2 3
2 3 4
2 1 1
2 3 4
3 4 5
## 输出样例
1061109567
## 提示
\ No newline at end of file
#include <iostream>
#include <cstring>
#include <queue>
#include <climits>
using namespace std;
const int N = 1e3 + 10, M = 2e6 + 10;
int h[N], e[M], ne[M], w[M], idx;
int dist[N];
int n, m, k;
bool st[N]; // 标记数组
// 初始化
void init() {
memset(h, -1, sizeof h);
idx = 0;
}
// 添加一条边
void add(int a, int b, int c) {
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
// Dijkstra 算法求最短路径
void dijkstra(int s) {
memset(dist, 0x3f, sizeof dist);
dist[s] = 0;
priority_queue<pair<int, int>> q; // 小根堆
q.push({0, s});
while (q.size()) {
auto t = q.top();
q.pop();
int ver = t.second, distance = t.first;
if (st[ver]) continue;
st[ver] = true;
// 更新所有 ver 相邻的点
for (int i = h[ver]; ~i; i = ne[i]) {
int j = e[i];
if (dist[j] > distance + w[i]) {
dist[j] = distance + w[i];
q.push({dist[j], j});
}
}
}
}
int main() {
// 读入
scanf("%d%d%d", &n, &m, &k);
init();
// 建图
while (m--) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, c);
}
// 求最短路径
dijkstra(1);
// 输出最短路径
printf("%d\n", dist[n]);
return 0;
}
2 4 7
2 3 4
2 3 4
1 2 3
4 5 6
\ No newline at end of file
5 10 6
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
5 2 3
2 3 4
2 1 1
2 3 4
3 4 5
\ No newline at end of file
3 3 40
1 2 3
2 3 4
5 3 2
\ No newline at end of file
3 3 40
1 2 3
2 3 3
5 6 7
\ No newline at end of file
2 4 30
5 1 2
5 2 2
5 3 2
\ No newline at end of file
6 3 4
2 1 4
3 4 2
1 2 1
\ No newline at end of file
3 6 5
1 2 3
2 3 6
5 3 1
5 1 2
5 2 2
5 2 2
\ No newline at end of file
1 3 5
3 9 0
2 4 0
4 1 2
\ No newline at end of file
# 柯里昂家族树
在《教父》中,柯里昂家族是一个著名的黑手党家族,由家族的首领柯里昂(Don Corleone)和他的四个儿子构成。柯里昂家族的家谱是一棵二叉树,其中柯里昂是根节点,他的儿子们分别是他的左儿子和右儿子。
现在,你需要编写一个程序,读入一个包含若干行的字符串,每行表示一个人的信息。每行的格式如下:
<人物名称> is the <父亲名称>'s <儿子>
其中,人物名称是一个不包含空格的字符串,父亲名称和儿子也是一个不包含空格的字符串。
例如,下面是一棵柯里昂家族的家谱:
Don Corleone is the Don's son
Fredo Corleone is the Don's son
Michael Corleone is the Don's son
Tom Hagen is the Don's adopted son
你的程序需要按照这些信息建立一棵二叉树,并输出这棵树的前序遍历。
例如,对于上面的家谱,输出应该是:
Don Corleone Fredo Corleone Michael Corleone Tom Hagen
你的程序需要支持以下功能:
- 建立二叉树。你需要定义一个结构体表示一个节点,包含人物名称、左儿子和右儿子三个字段。你需要编写一个函数,读入若干行字符串,根据字符串中的信息建立二叉树。
- 输出前序遍历。你需要编写一个函数,输出给定二叉树的前序遍历。
## 输入格式
输入的第一行包含一个整数 n(1 <= n <= 1000),表示字符串的行数。
接下来的 n 行,每行包含一个字符串,表示一个人的信息。字符串的格式如上文所述。
## 输出格式
输出给定二叉树的前序遍历,每个人物名称占一行。
## 输入样例
4
Don Corleone is the Don's son
Fredo Corleone is the Don's son
Michael Corleone is the Don's son
Tom Hagen is the Don's adopted son
## 输出样例
Don Corleone
Fredo Corleone
Michael Corleone
Tom Hagen
## 提示
1. 人物名称和父亲名称均由不超过 100 个字符组成,且不包含空格;
2. 儿子的值可能是 "son" 或 "adopted son";
3. 输入中可能会出现重名的人物,你的程序需要处理这种情况;
4. 输入的字符串保证是合法的,不会出现父亲名称找不到的情况。
\ No newline at end of file
#include <iostream>
#include <unordered_map>
using namespace std;
//定义结构体表示一个节点
struct Node {
string name;
Node* left;
Node* right;
};
//建立二叉树的函数
Node* build_tree(int n) {
// 建立根节点
string line;
getline(cin, line);
stringstream ss(line);
string name, is, the, father, s;
ss >> name >> is >> the >> father >> s;
Node* root = new Node{name, nullptr, nullptr};
// 建立剩余的节点
for (int i = 1; i < n; i++) {
getline(cin, line);
stringstream ss(line);
string name, is, the, father, s;
ss >> name >> is >> the >> father >> s;
Node* cur = root;
while (true) {
if (father == cur->name && s == "son") {
if (cur->left == nullptr) {
cur->left = new Node{name, nullptr, nullptr};
break;
} else if (cur->right == nullptr) {
cur->right = new Node{name, nullptr, nullptr};
break;
} else {
cur = cur->left;
}
} else {
cur = cur->right;
}
}
}
return root;
}
//输出前序遍历的函数
void preorder(Node* root) {
cout << root->name << " ";
if (root->left != nullptr) preorder(root->left);
if (root->right != nullptr) preorder(root->right);
}
int main() {
int n;
cin >> n;
cin.ignore();
Node* root = build_tree(n);
preorder(root);
return 0;
}
5
Don Corleone is the Don's son
Fredo Corleone is the Don's son
Michael Corleone is the Don's son
Tom Hagen is the Don's adopted son
Santino Corleone is the Michael's son
\ No newline at end of file
Don Corleone
Fredo Corleone
Michael Corleone
Tom Hagen
Santino Corleone
\ No newline at end of file
3
Don Corleone is the Don's son
Fredo Corleone is the Don's son
Tom Hagen is the Fredo's son
\ No newline at end of file
Don Corleone
Fredo Corleone
Tom Hagen
\ No newline at end of file
1
Don Corleone is the Don's son
\ No newline at end of file
3
Don Corleone is the Don's son
Fredo Corleone is the Don's son
Tom Hagen is the Fredo's son
\ No newline at end of file
Don Corleone
Fredo Corleone
Tom Hagen
\ No newline at end of file
3
Don Corleone is the Don's son
Fredo Corleone is the Don's son
Tom Hagen is the Don's adopted son
\ No newline at end of file
Don Corleone
Fredo Corleone
Tom Hagen
\ No newline at end of file
2
Don Corleone is the Don's son
Michael Corleone is the Don's son
\ No newline at end of file
Don Corleone
Michael Corleone
\ No newline at end of file
2
Don Corleone is the Don's son
Tom Hagen is the Don's adopted son
\ No newline at end of file
3
Don Corleone is the Don's son
Fredo Corleone is the Don's son
Tom Hagen is the Don's adopted son
\ No newline at end of file
Don Corleone
Fredo Corleone
Tom Hagen
\ No newline at end of file
4
Don Corleone is the Don's son
Fredo Corleone is the Don's son
Michael Corleone is the Don's son
Tom Hagen is the Don's adopted son
\ No newline at end of file
Don Corleone
Fredo Corleone
Michael Corleone
Tom Hagen
\ No newline at end of file
5
Don Corleone is the Don's son
Fredo Corleone is the Don's son
Michael Corleone is the Don's son
Tom Hagen is the Don's adopted son
Santino Corleone is the Michael's son
\ No newline at end of file
Don Corleone
Fredo Corleone
Michael Corleone
Tom Hagen
Santino Corleone
\ No newline at end of file
# 山洞珍宝
在一端山路中,有一个神秘的山洞。这个山洞里有许多神奇的宝藏。
你在山洞外发现了一个奇怪的墙壁,上面有一个神秘的符号,你需要通过一定的方法来解码这个符号,只有解码这个符号后你才能进入这个山洞。
经过一番推导,你发现这个符号是一个由 0 和 1 组成的字符串,每个字符都有一个权值,分别是 $0,1,2,3,4,5,6,7,8,9$。
你需要从这个字符串中找到一个最大的子串,使得该子串的权值之和最大。
给定一个字符串 $S$,其长度不超过 $10^5$,且只包含字符 '0' 和 '1'。
请你找到字符串 $S$ 中权值和最大的子串。
## 输入描述
第一行包含一个字符串 $S$。
## 输出描述
输出一行,包含一个整数,表示最大的子串的权值和。
## 输入样例
0110100110110100
## 输出样例
35
## 提示
\ No newline at end of file
#include <iostream>
#include <string>
using namespace std;
const int N = 1e5 + 10;
int sum[N]; // sum[i] 表示字符串 s 的前 i 个字符的权值和
int main() {
string s;
cin >> s;
int n = s.size();
// 计算权值和
for (int i = 0; i < n; i++) sum[i + 1] = sum[i] + (s[i] - '0');
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j <= n; j++) {
// 计算字符串 s[i...j] 的权值和
int w = sum[j] - sum[i];
ans = max(ans, w);
}
}
cout << ans << endl;
return 0;
}
100000000010000000001
\ No newline at end of file
111110000101010101101000011
\ No newline at end of file
1010101000001111010101001
\ No newline at end of file
111111100000000011010101001101
\ No newline at end of file
10101010101001011111111
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册