Skip to content

Commit e805d59

Browse files
committed
PAT真题解答:B1015,B1019
1 parent 4cb701e commit e805d59

File tree

3 files changed

+232
-1
lines changed

3 files changed

+232
-1
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
1015 德才论(25 分)
3+
4+
宋代史学家司马光在《资治通鉴》中有一段著名的“德才论”:“是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜德谓之小人。凡取人之术,苟不得圣人,君子而与之,与其得小人,不若得愚人。”
5+
6+
现给出一批考生的德才分数,请根据司马光的理论给出录取排名。
7+
输入格式:
8+
9+
输入第一行给出 3 个正整数,分别为:N(≤10​5​​),即考生总数;L(≥60),为录取最低分数线,即德分和才分均不低于 L 的考生才有资格被考虑录取;H(<100),为优先录取线——德分和才分均不低于此线的被定义为“才德全尽”,此类考生按德才总分从高到低排序;才分不到但德分到线的一类考生属于“德胜才”,也按总分排序,但排在第一类考生之后;德才分均低于 H,但是德分不低于才分的考生属于“才德兼亡”但尚有“德胜才”者,按总分排序,但排在第二类考生之后;其他达到最低线 L 的考生也按总分排序,但排在第三类考生之后。
10+
11+
随后 N 行,每行给出一位考生的信息,包括:准考证号 德分 才分,其中准考证号为 8 位整数,德才分为区间 [0, 100] 内的整数。数字间以空格分隔。
12+
输出格式:
13+
14+
输出第一行首先给出达到最低分数线的考生人数 M,随后 M 行,每行按照输入格式输出一位考生的信息,考生按输入中说明的规则从高到低排序。当某类考生中有多人总分相同时,按其德分降序排列;若德分也并列,则按准考证号的升序输出。
15+
输入样例:
16+
17+
14 60 80
18+
10000001 64 90
19+
10000002 90 60
20+
10000011 85 80
21+
10000003 85 80
22+
10000004 80 85
23+
10000005 82 77
24+
10000006 83 76
25+
10000007 90 78
26+
10000008 75 79
27+
10000009 59 90
28+
10000010 88 45
29+
10000012 80 100
30+
10000013 90 99
31+
10000014 66 60
32+
33+
输出样例:
34+
35+
12
36+
10000013 90 99
37+
10000012 80 100
38+
10000003 85 80
39+
10000011 85 80
40+
10000004 80 85
41+
10000007 90 78
42+
10000006 83 76
43+
10000005 82 77
44+
10000002 90 60
45+
10000014 66 60
46+
10000008 75 79
47+
10000001 64 90
48+
49+
*/
50+
51+
#include <stdio.h>
52+
#include <stdlib.h>
53+
54+
typedef struct {
55+
int number;
56+
int de;
57+
int cai;
58+
int total;
59+
} ss;
60+
61+
int m_cmp(const void *a, const void *b);
62+
63+
int main()
64+
{
65+
ss t[4][100000];
66+
67+
int total, low, high, i, j;
68+
int number, de, cai, total_n;
69+
int hg[4], index;
70+
int hege = 0;
71+
72+
if (!scanf("%d %d %d", &total, &low, &high)) {
73+
return 1;
74+
}
75+
76+
for (i = 0; i < total; i++) {
77+
if (!scanf("%d %d %d", &number, &de, &cai)) {
78+
return 1;
79+
}
80+
if (de >= low && cai >= low) {
81+
total_n = de + cai;
82+
83+
if (de >= high && cai >= high) {
84+
index = 0;
85+
} else if (de >= high && cai < high) {
86+
index = 1;
87+
} else if (de >= cai) {
88+
index = 2;
89+
} else {
90+
index = 3;
91+
}
92+
t[index][hg[index]].number = number;
93+
t[index][hg[index]].de = de;
94+
t[index][hg[index]].cai = cai;
95+
t[index][hg[index]].total = total_n;
96+
hg[index]++;
97+
hege++;
98+
}
99+
}
100+
101+
for (i = 0; i < 4; i++) {
102+
qsort(&t[i], hg[i], sizeof(ss), m_cmp);
103+
}
104+
printf("%d\n", hege);
105+
for (i = 0; i < 4; i++) {
106+
for (j = 0; j < hg[i]; j++) {
107+
printf("%d %d %d\n", t[i][j].number, t[i][j].de, t[i][j].cai);
108+
}
109+
}
110+
return 0;
111+
}
112+
113+
int m_cmp(const void *a, const void *b)
114+
{
115+
ss *c = (ss *)a;
116+
ss *d = (ss *)b;
117+
if (c->total != d->total) {
118+
return c->total < d->total;
119+
} else if (c->de != d->de) {
120+
return c->de < d->de;
121+
} else {
122+
return c->number > d->number;
123+
}
124+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
1019 数字黑洞(20 分)
3+
4+
给定任一个各位数字不完全相同的 4 位正整数,如果我们先把 4 个数字按非递增排序,再按非递减排序,然后用第 1 个数字减第 2 个数字,将得到一个新的数字。一直重复这样做,我们很快会停在有“数字黑洞”之称的 6174,这个神奇的数字也叫 Kaprekar 常数。
5+
6+
例如,我们从6767开始,将得到
7+
8+
7766 - 6677 = 1089
9+
9810 - 0189 = 9621
10+
9621 - 1269 = 8352
11+
8532 - 2358 = 6174
12+
7641 - 1467 = 6174
13+
... ...
14+
15+
现给定任意 4 位正整数,请编写程序演示到达黑洞的过程。
16+
输入格式:
17+
18+
输入给出一个 (0,10​4​​) 区间内的正整数 N。
19+
输出格式:
20+
21+
如果 N 的 4 位数字全相等,则在一行内输出 N - N = 0000;否则将计算的每一步在一行内输出,直到 6174 作为差出现,输出格式见样例。注意每个数字按 4 位数格式输出。
22+
输入样例 1:
23+
24+
6767
25+
26+
输出样例 1:
27+
28+
7766 - 6677 = 1089
29+
9810 - 0189 = 9621
30+
9621 - 1269 = 8352
31+
8532 - 2358 = 6174
32+
33+
输入样例 2:
34+
35+
2222
36+
37+
输出样例 2:
38+
39+
2222 - 2222 = 0000
40+
41+
*/
42+
43+
#include <stdio.h>
44+
#include <stdlib.h>
45+
#include <string.h>
46+
47+
void m_sort(char *ori, int up);
48+
49+
int main ()
50+
{
51+
char big[5], small[5], r[5];
52+
int big_i, small_i, r_i = 0;
53+
54+
if (!scanf("%d", &r_i)) {
55+
return 1;
56+
}
57+
58+
do {
59+
sprintf(r, "%04d", r_i);
60+
strcpy(big, r);
61+
strcpy(small, big);
62+
m_sort(big, 0);
63+
m_sort(small, 1);
64+
big_i = atoi(big);
65+
small_i = atoi(small);
66+
r_i = big_i - small_i;
67+
printf("%04d - %04d = %04d\n", big_i, small_i, r_i);
68+
69+
} while ((r_i != 6174) && (big_i != small_i));
70+
71+
return 0;
72+
}
73+
74+
void m_sort(char *ori, int up)
75+
{
76+
int i, j, str_len, t;
77+
78+
str_len = strlen(ori);
79+
80+
for (i = 0; i < str_len; i++) {
81+
for (j = 0; j < str_len - i - 1; j++) {
82+
if (up) {
83+
if (ori[j] > ori[j+1]) {
84+
t = ori[j];
85+
ori[j] = ori[j+1];
86+
ori[j+1] = t;
87+
}
88+
} else {
89+
if (ori[j] < ori[j+1]) {
90+
t = ori[j];
91+
ori[j] = ori[j+1];
92+
ori[j+1] = t;
93+
}
94+
}
95+
96+
}
97+
}
98+
}
99+

PhpCodes/DataStructureAndAlgorithm/BPlusTree/BPlusTree.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,24 @@
77

88
class BTNode
99
{
10+
public $id;
11+
public $parent;
12+
public $indexMap = [];
1013

14+
public function __construct()
15+
{
16+
$this->id = uniqid();
17+
}
1118
}
1219

1320
class innerNode extends BTNode
1421
{
15-
22+
public $children = [];
1623
}
1724

1825
class LeafNode extends BTNode
1926
{
27+
public $nextNode;
2028

2129
}
2230
class BPlusTree

0 commit comments

Comments
 (0)