2017级《程序设计基础(B)II》期末机考题解(Fish出题部分)

The Legend of Zelda

首先一个点 $(x, y)$ 如果 $x \le x_1$ or $y \le y_1 $ or $ x \ge x_2 $ or $y\ge y_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
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
#include <stdio.h>
#include <string.h>

#define MAX 1000

struct Node {
int x, y;
} data[MAX];

int compare(Node a, Node b) {
if (a.x != b.x) return a.x - b.x;
return a.y - b.y;
}

int max(int a, int b) {
return a > b ? a : b;
}

void quick_sort(Node data[], int l, int r) {
Node key = data[l];
int i = l, j = r;
while (i < j) {
while (i < j && compare(data[j], key) >= 0) j--;
data[i] = data[j];
while (i < j && compare(data[i], key) <= 0) i++;
data[j] = data[i];
}
data[i] = key;
if (l < i) quick_sort(data, l, i - 1);
if (i < r) quick_sort(data, i + 1, r);
}

int dp[MAX];

int x1, x2, y1, y2;

bool check(int x, int y) {
return x1 < x && x < x2 && y1 < y && y < y2;
}

int main() {
while (~scanf("%d%d%d%d", &x1, &y1, &x2, &y2)) {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d%d", &data[i].x, &data[i].y);
}
quick_sort(data, 1, n);
memset(dp, 0, sizeof(dp));
int ans = 0;
for (int i = 1; i <= n; i++) {
if (!check(data[i].x, data[i].y)) continue;
for (int j = 0; j < i; j++) {
if (data[j].y >= data[i].y) continue;
dp[i] = max(dp[i], dp[j] + 1);
}
ans = max(ans, dp[i]);
}
printf("%d\n", ans + 2);
}
}

Dragon Quest

按题意模拟即可,暴力递归写。可能需要注意下,释放魔法后是立即生效。

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
#include <stdio.h>
#include <string.h>

int ans = 0;
int H1, A1, D1, H2, A2, D2;

int max(int a, int b) {
return a > b ? a : b;
}

void Boss(int, int, int, int, int);
void Player(int, int, int, int, int);

//玩家操作
void Player(int H, int A, int D, int BH, int dep) {
//如果进行超过五回合就结束
if (dep > 5) return;
//如果玩家HP小于等于0结束游戏
if (H <= 0) return;
//计算造成的伤害
int hurt = max(0, A - D2);
//玩家进行攻击
Boss(H, A, D, BH - hurt, dep);
//玩家释放加强攻击魔法
Boss(H, A + 5, D, BH, dep);
//玩家释放加强防御魔法
Boss(H, A, D + 5, BH, dep);
}

//Boss操作
void Boss(int H, int A, int D, int BH, int dep) {
//Boss血量小于等于0,玩家胜利
if (BH <= 0) {
ans++;
return;
}
//计算对玩家造成的伤害
int hurt = max(0, A2 - D);
//Boss进行攻击
Player(H - hurt, A, D, BH, dep + 1);
}

int main() {
while (~scanf("%d %d %d %d %d %d", &H1, &A1, &D1, &H2, &A2, &D2)) {
ans = 0;
//玩家先进行操作
Player(H1, A1, D1, H2, 1);
printf("%d\n", ans);
}
}