PassThePAT


  • 首页

  • 标签

  • 分类

  • 归档

  • 搜索

1078 Hashing

发表于 2020-05-31

给出散列表长度以及插入的元素,问插入位置。
重点在于判断是否为素数(保证散列表长度为素数),以及二次探测法(寻找元素插入位置)。

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 <bits/stdc++.h>
using namespace std;

int m, n, hashTable[10010];

bool isPrime (int num) {
if (num == 1) return false;
for (int i = 2; i * i <= num; i++) {
if (num %i == 0) return false;
}
return true;
}

void insert(int key) {
for (int step = 0; step < m; step++) {
int index = (key + step * step) % m;
if (hashTable[index] == 0) {
hashTable[index] = 1;
cout << index;
return;
}
}
cout << "-";
}

int main() {
cin >> m >> n;
while (!isPrime(m)) m++;
for (int i = 0; i < n; i++) {
int key;
cin >> key;
if (i != 0) cout << " ";
insert(key);
}

return 0;
}

1077 Kuchiguse

发表于 2020-05-31

重点在于For each test case, print in one line the kuchiguse of the character, i.e., the longest common suffix of all N lines. If there is no such suffix, write nai.
求给出字符串的公共后缀。

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
#include <bits/stdc++.h>
using namespace std;

int main() {
int n;
string result;
scanf("%d\n", &n);
for (int i = 0; i < n; i++) {
string s;
getline(cin, s);
int len = s.length();
reverse(s.begin(), s.end());
if (i == 0) {
result = s;
}
else {
int lenResult = result.length();
int minlen = min(len, lenResult);
for (int j = 0; j < minlen; j++) {
if (result[j] != s[j]) {
result = s.substr(0, j);
break;
}
}
}
}
reverse(result.begin(), result.end());
if (result.length() == 0) {
cout << "nai";
}
else {
cout << result;
}
cout << endl;

return 0;
}

// scanf“%d”少了个\n出错

3
Itai nyan~
Ninjin wa iyadanyan~
uhhh nyan~

nyan~

1076 Forwards on Weibo

发表于 2020-05-31

1075 PAT Judge

发表于 2020-05-31

1074 Reversing Linked List

发表于 2020-05-31

1073 Scientific Notation

发表于 2020-05-31

1072 Gas Station

发表于 2020-05-31

1071 Speech Patterns

发表于 2020-05-31

输出出现最多的那个字符串(字母和数字的组合),不区分大小写。
因为输入中可能有空格,因此用getline(cin, s);输入。
空字符串的个数不应该被统计,因此t长度不为0。

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

int main() {
string s, t;
map<string, int> m;
getline(cin, s);
for (int i = 0; i < s.length(); i++) {
if (isalnum(s[i])) {
s[i] = tolower(s[i]);
t += s[i];
}
if (!isalnum(s[i]) || i == s.length() - 1) {
if (t.length() != 0) m[t]++;
t = "";
}
}
int resultMax = 0;
string resultStr = "";
for (auto it = m.begin(); it != m.end(); it++) {
if (it->second > resultMax) {
resultMax = it->second;
resultStr = it->first;
}
}
cout << resultStr << " " << resultMax << endl;

return 0;
}

1070 Mooncake

发表于 2020-05-31

给出仓库容量,不同种类月饼的库存和总价值,问把仓库装满后,最高的货值是多少。采购单价高的,若有空余,采购次高者,以此类推。

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

struct mooncake {
float price, amount, unitPrice;
};

int cmp(mooncake a, mooncake b) {
return a.unitPrice > b.unitPrice;
}

int main() {
int n, total;
cin >> n >> total;
vector<mooncake> cake(n);
for (int i = 0; i < n; i++) cin >> cake[i].amount;
for (int i = 0; i < n; i++) cin >> cake[i].price;
for (int i = 0; i < n; i++) {
cake[i].unitPrice = cake[i].price / cake[i].amount;
}
sort(cake.begin(), cake.end(), cmp);
float res = 0.0;
for (int i = 0; i < n; i++) {
if (total >= cake[i].amount) {
res += cake[i].price;
} else {
res += cake[i].unitPrice * total;
break;
}
total -= cake[i].amount;
}
printf("%.2f", res);

return 0;
}

1069 The Black Hole of Numbers

发表于 2020-05-31
1…8910…16

e5

158 日志
5 标签
© 2022 e5
由 Hexo 强力驱动
|
主题 — NexT.Mist v5.1.4