系列文章目录
备考PAT&考研2021.6.27
PAT乙级刷题
前言
pat乙级刷题训练&考研&计算机二级(7&8月)
争取每天3道题左右,一周5次以上。
AC就算成功。
编译器&语言:c++(c)&clang++
一、 数字分类
1.题目
2.代码
思路:数组&结构体的简单利用
#include <cstdio>
int main() {
int n, temp;
int count[5] = { 0 };
int ans[5] = { 0 };
scanf("%d",&n);
for (int i = 0; i < n; i++) {
scanf("%d", &temp);
if (temp % 5 == 0) {
if (temp % 2 == 0) {
ans[0] += temp;
count[0]++;
}
}
else if (temp % 5 == 4) {
if (temp > ans[4]) {
ans[4] = temp;
}
count[4]++;
}
else if (temp % 5 == 1) {
count[1]++;
if (count[1] % 2 != 0) {
ans[1] += temp;
}
else {
ans[1] -= temp;
}
}
else if (temp % 5 == 2) {
count[2]++;
}
else {
ans[3] += temp;
count[3]++;
}
}
if (count[0] != 0) printf("%d ", ans[0]);
else printf("N ");
if (count[1] != 0) printf("%d ", ans[1]);
else printf("N ");
if (count[2] != 0) printf("%d ", count[2]);
else printf("N ");
if (count[3] != 0) printf("%.1f ", (double)ans[3] / count[3]);
else printf("N ");
if (count[4] != 0) printf("%d", ans[4]);
else printf("N");
return 0;
}
二、1046 Shortest Distance (20 分)
1.题目
The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed to tell the shortest distance between any pair of exits.
2.代码
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 100005;
int dis[maxn], a[maxn];
int main() {
int sum = 0, query, n, left, right;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
sum += a[i];
dis[i] = sum;
}
scanf("%d", &query);
for (int i = 0; i < query; i++) {
scanf("%d%d", &left, &right);
if (left > right) swap(left, right);
int temp = dis[right-1]-dis[left-1];
printf("%d\n", min(temp, sum - temp));
}
return 0;
}
三、1041 考试座位号 (15 分)
1.题目
每个 PAT 考生在参加考试时都会被分配两个座位号,一个是试机座位,一个是考试座位。正常情况下,考生在入场时先得到试机座位号码,入座进入试机状态后,系统会显示该考生的考试座位号码,考试时考生需要换到考试座位就座。但有些考生迟到了,试机已经结束,他们只能拿着领到的试机座位号码求助于你,从后台查出他们的考试座位号码。
2.代码
#include <cstdio>
const int maxn = 1010;
struct studet {
long long id;
int examseat;
}testseat[maxn];
int main() {
int n, m, seat, examseat;
long long id;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%lld %d %d", &id, &seat, &examseat);
testseat[seat].id = id;
testseat[seat].examseat = examseat;
}
scanf("%d", &m);
for (int i = 0; i < m; i++) {
scanf("%d", &seat);
printf("%lld %d\n", testseat[seat].id, testseat[seat].examseat);
}
return 0;
}
总结
继续康复训练