PassThePAT


  • 首页

  • 标签

  • 分类

  • 归档

  • 搜索

1058 A+B in Hogwarts

发表于 2020-05-31

货币转换,“Seventeen silver Sickles to a Galleon and twenty-nine Knuts to a Sickle, it’s easy enough.”
sum = sum % (17 29)的值最大为28+1629 < 17*29。

`c++

#include
using namespace std;

int main() {
long long a, b, c, d, e, f;
scanf(“%lld.%lld.%lld %lld.%lld.%lld”, &a, &b, &c, &d, &e, &f);
long long sum = c + b 29 + a 17 29 + f + e 29 + d 17 29;
long long g = sum / (17 29);
sum = sum % (17
29);
printf(“%lld.%lld.%lld”, g, sum / 29, sum % 29);

return 0;

}

3.2.1 10.16.27
14.1.28

1057 Stack

发表于 2020-05-31

1056 Mice and Rice

发表于 2020-05-31

输入n为老鼠数量,g为每一组老鼠的最大值,之后从0到n-1个老鼠的体重,最后是老鼠体重的比较序列。
对于每一组老鼠,选出最重的那个,晋级下一轮,本轮的其他老鼠名次相同,直到选出第一名。

结构体node表示老鼠,里面包括weight重量,index是按照排名后的顺序的老鼠的下标,index0是排名前老鼠的下标。rank是最终要输出的老鼠的排名。

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
62
63
#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
struct node {
int weight, index, rank, index0;
};
bool cmp1(node a, node b) {
return a.index0 < b.index0;
}
int main() {
int n, g, num;
scanf("%d%d", &n, &g);
vector<int> v(n);
vector<node> w(n);
for(int i = 0; i < n; i++)
scanf("%d", &v[i]);
for(int i = 0; i < n; i++) {
scanf("%d", &num);
w[i].weight = v[num];
w[i].index = i;
w[i].index0 = num;
}
queue<node> q;
for(int i = 0; i < n; i++)
q.push(w[i]);
while(!q.empty()) {
int size = q.size();
if(size == 1) {
node temp = q.front();
w[temp.index].rank = 1;
break;
}
int group = size / g;
if(size % g != 0)
group += 1;
node maxnode;
int maxn = -1, cnt = 0;
for(int i = 0; i < size; i++) {
node temp = q.front();
w[temp.index].rank = group + 1;
q.pop();
cnt++;
if(temp.weight > maxn) {
maxn = temp.weight;
maxnode = temp;
}
if(cnt == g || i == size - 1) {
cnt = 0;
maxn = -1;
q.push(maxnode);
}
// 把本组的获胜者放入比较队列,并且重置准备检查下一组
}
}
sort(w.begin(), w.end(), cmp1);
for(int i = 0; i < n; i++) {
if(i != 0) printf(" ");
printf("%d", w[i].rank);
}
return 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <vector>
using namespace std;

int main(){
int n, m, x;
cin >> n >> m;
vector<int> mice(n);
vector<int> order(n);
int rank[n];
fill(rank,rank+n,0);

for (int i = 0; i < n; ++i) {
cin>>mice[i];
}
for (int i = 0; i < n; ++i) {
cin>>order[i];
}
vector<int> res;
int Max, idex, group;
while (order.size()>1) {
group = order.size() / m + 1;
if ((order.size() % m != 0) ) ++group;
for (int i = 0; i < order.size(); ++i) {
if (i%m == 0) {
Max = -1;
if (i != 0) {
res.push_back(idex);
}
}
if (Max < mice[order[i]]) {
Max = mice[order[i]];
idex = order[i];
}
rank[order[i]] = group;
}
res.push_back(idex);
order = res;
res.clear();
}
rank[order[0]] = 1;
for (int i = 0; i < n; ++i) {
if (i != 0) cout << ' ';
cout << rank[i];
}
return 0;
}

原文链接:https://blog.csdn.net/CV_Jason/java/article/details/85238006

1055 The World's Richest

发表于 2020-05-31

模拟一个福布斯分类排行榜。给N个人的名字,年龄,财富。
给K个筛选标准,包括M最大输出人数(最多100人),年龄范围。

之后按照:The outputs must be in non-increasing order of the net worths. In case there are equal worths, it must be in non-decreasing order of the ages. If both worths and ages are the same, then the output must be in non-decreasing alphabetical order of the names. It is guaranteed that there is no two persons share all the same of the three pieces of information. In case no one is found, output None.输出。

钱多的优先,年龄小的优先,名字字典序小的优先。

为什么vt[i].name没有&?
首先说明 %s格式符 表示用来输入出一个字符串 而字符串是以数组的形式的存储的
c语言中数组名代表该数组的起始地址。 此处,a为数组名 代表的是首地址,所以就不用取地址符了, 再用取地址符号 就重复了 请注意与%c的区别 理解就好啦。

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;

struct rich {
char name[10];
int age, money;
};

int cmp(rich a, rich b) {
if (a.money != b.money) {
return a.money > b.money;
} else if (a.age != b.age) {
return a.age < b.age;
} else {
return (strcmp(a.name, b.name) < 0);
}
}

int main() {
int n, k, num, amin, amax;
scanf("%d %d", &n, &k);
vector<rich> vt(n), v;
vector<int> book(205, 0);
for (int i = 0; i < n; i++) {
scanf("%s %d %d", vt[i].name, &vt[i].age, &vt[i].money);
}
sort(vt.begin(), vt.end(), cmp);
// 输入数据,排序
for (int i = 0; i < n; i++) {
if (book[vt[i].age] < 100) { // book[vt[i].age]为100说明有100个人了
v.push_back(vt[i]);
book[vt[i].age]++;
}
}
// 筛去每个年龄层100名后的人以及统计各年龄富豪人数
for (int i = 0; i < k; i++) {
scanf("%d %d %d", &num, &amin, &amax);
vector<rich> t;
for (int j = 0; j < v.size(); j++) {
if (v[j].age >= amin && v[j].age <= amax) {
t.push_back(v[j]);
}
}
// 符合要求的放入临时向量t
if (i != 0) printf("\n");
printf("Case #%d:", i + 1);
int flag = 0;
for (int j = 0; j < num && j < t.size(); j++) {
printf("\n%s %d %d", t[j].name, t[j].age, t[j].money);
flag = 1;
}
if (flag == 0) printf("\nNone");
}

return 0;

}

12 4
Zoe_Bill 35 2333
Bob_Volk 24 5888
Anny_Cin 95 999999
Williams 30 -22
Cindy 76 76000
Alice 18 88888
Joe_Mike 32 3222
Michael 5 300000
Rosemary 40 5888
Dobby 24 5888
Billy 24 5888
Nobody 5 0
4 15 45
4 30 35
4 5 95
1 45 50

Case #1:
Alice 18 88888
Billy 24 5888
Bob_Volk 24 5888
Dobby 24 5888
Case #2:
Joe_Mike 32 3222
Zoe_Bill 35 2333
Williams 30 -22
Case #3:
Anny_Cin 95 999999
Michael 5 300000
Alice 18 88888
Cindy 76 76000
Case #4:
None

1054 The Dominant Color

发表于 2020-05-31

注意输入的分辨率是M列N行,虽然和平时输入有点不一样但和像素是一样的,1024x768,1024是长,768是宽。

`c++

#include

#include
using namespace std;

int main() {
int m, n;
scanf(“%d %d”, &m, &n);
map<int, int> pixel;
int half = n * m / 2;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) { //m列,即每行有m个
int t;
scanf(“%d”, &t);
pixel[t]++;
if (pixel[t] > half) {
printf(“%d”, t);
return 0;
}
}
}

return 0;

}

1053 Path of Equal Weight

发表于 2020-05-31

模拟一个福布斯分类排行榜。给N个人的名字,年龄,财富。
给K个筛选标准,包括M最大输出人数(最多100人),年龄范围。

之后按照:The outputs must be in non-increasing order of the net worths. In case there are equal worths, it must be in non-decreasing order of the ages. If both worths and ages are the same, then the output must be in non-decreasing alphabetical order of the names. It is guaranteed that there is no two persons share all the same of the three pieces of information. In case no one is found, output None.输出。

钱多的优先,年龄小的优先,名字字典序小的优先。

为什么vt[i].name没有&?
首先说明 %s格式符 表示用来输入出一个字符串 而字符串是以数组的形式的存储的
c语言中数组名代表该数组的起始地址。 此处,a为数组名 代表的是首地址,所以就不用取地址符了, 再用取地址符号 就重复了 请注意与%c的区别 理解就好啦。

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;

struct rich {
char name[10];
int age, money;
};

int cmp(rich a, rich b) {
if (a.money != b.money) {
return a.money > b.money;
} else if (a.age != b.age) {
return a.age < b.age;
} else {
return (strcmp(a.name, b.name) < 0);
}
}

int main() {
int n, k, num, amin, amax;
scanf("%d %d", &n, &k);
vector<rich> vt(n), v;
vector<int> book(205, 0);
for (int i = 0; i < n; i++) {
scanf("%s %d %d", vt[i].name, &vt[i].age, &vt[i].money);
}
sort(vt.begin(), vt.end(), cmp);
// 输入数据,排序
for (int i = 0; i < n; i++) {
if (book[vt[i].age] < 100) { // book[vt[i].age]为100说明有100个人了
v.push_back(vt[i]);
book[vt[i].age]++;
}
}
// 筛去每个年龄层100名后的人以及统计各年龄富豪人数
for (int i = 0; i < k; i++) {
scanf("%d %d %d", &num, &amin, &amax);
vector<rich> t;
for (int j = 0; j < v.size(); j++) {
if (v[j].age >= amin && v[j].age <= amax) {
t.push_back(v[j]);
}
}
// 符合要求的放入临时向量t
if (i != 0) printf("\n");
printf("Case #%d:", i + 1);
int flag = 0;
for (int j = 0; j < num && j < t.size(); j++) {
printf("\n%s %d %d", t[j].name, t[j].age, t[j].money);
flag = 1;
}
if (flag == 0) printf("\nNone");
}

return 0;

}

12 4
Zoe_Bill 35 2333
Bob_Volk 24 5888
Anny_Cin 95 999999
Williams 30 -22
Cindy 76 76000
Alice 18 88888
Joe_Mike 32 3222
Michael 5 300000
Rosemary 40 5888
Dobby 24 5888
Billy 24 5888
Nobody 5 0
4 15 45
4 30 35
4 5 95
1 45 50

Case #1:
Alice 18 88888
Billy 24 5888
Bob_Volk 24 5888
Dobby 24 5888
Case #2:
Joe_Mike 32 3222
Zoe_Bill 35 2333
Williams 30 -22
Case #3:
Anny_Cin 95 999999
Michael 5 300000
Alice 18 88888
Cindy 76 76000
Case #4:
None

1052 Linked List Sorting

发表于 2020-05-31

给出n个节点,把链表中的节点按照key值从小到大排列。

`c++

#include

#include
using namespace std;
struct NODE {
int address, key, next;
bool flag;
}node[100000];
int cmp1(NODE a, NODE b) {
return !a.flag || !b.flag ? a.flag > b.flag : a.key < b.key;
// !a.flag || !b.flag为真即有节点不在链表里,往后放a.flag > b.flag,真1在前,假0在后
// 然后按照递增排列
}
int main() {
int n, cnt = 0, s, a, b, c;
scanf(“%d%d”, &n, &s);
for(int i = 0; i < n; i++) {
scanf(“%d%d%d”, &a, &b, &c);
node[a] = {a, b, c, false};
}
// 输入数据n个节点,s为root
for(int i = s; i != -1; i = node[i].next) {
node[i].flag = true;
cnt++;
}
// 链表中的节点标为true,因为可能节点并不存在于链上
if(cnt == 0) {
printf(“0 -1”);
} else {
sort(node, node + 100000, cmp1);
printf(“%d %05d\n”, cnt, node[0].address);
for(int i = 0; i < cnt; i++) {
printf(“%05d %d “, node[i].address, node[i].key);
if(i != cnt - 1)
printf(“%05d\n”, node[i + 1].address);
else
printf(“-1\n”);
}
}
return 0;
}

1051 Pop Sequence

发表于 2020-05-31

M,栈的最大容量,N出栈序列的长度,K个出栈序列等待检查。

`c++

#include

#include

#include

#include
using namespace std;

int main() {
int m, n, k;
scanf(“%d %d %d”, &m, &n, &k);
for (int i = 0; i < k; i++) {
bool flag = false;
stack s;
vector v(n + 1);
for (int j = 1; j <= n; j++) {
scanf(“%d”, &v[j]);
}
int cur = 1;
for (int j = 1; j <= n; j++) {
s.push(j);
if (s.size() > m) break;
while (!s.empty() && s.top() == v[cur]) {
s.pop();
cur++;
}
}
if (cur == n + 1) flag = true;
if (flag) printf(“YES\n”);
else printf(“NO\n”);
}

return 0;

}

1050 String Subtraction

发表于 2020-05-30

给2个字符串,要求输出S1-S2,结果不能出现S2中出现的字符。

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

char s1[10001], s2[10001];
int main() {
cin.getline(s1, 10001);
cin.getline(s2, 10001);
int len1 = strlen(s1);
int len2 = strlen(s2);
bool ASCII[300] = {false};
for (int i = 0; i< len2; i++) {
ASCII[s2[i]] = true;
}
for (int i = 0; i < len1; i++) {
if (ASCII[s1[i]] == false) {
printf("%c", s1[i]);
}
}

return 0;
}

// 因为输入会有空格,所以用cin.getline()来读取一行,用scanf遇到空格会停止

They are students.
aeiou

Thy r stdnts.

1049 Counting Ones

发表于 2020-05-30
1…101112…16

e5

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