pat2021年秋季甲级题解
作者:互联网
温馨提示:由于刚刚考完,我没弄到题目,之后有题目后再附上题目,这次先附上代码吧。本次代码使用c++来写
7-1:开数组,找地址,统计已开数组
#include<bits/stdc++.h>
using namespace std;
#pragma warning(disable:4996)
int n,k;
int arr[10001];
unordered_map<int, int> mp;
int main() {
scanf("%d %d", &n, &k);
fill(arr, arr + 10001, -1);
int sum = 0;
for (int i = 0; i < n; i++) {
int address, index;
scanf("%d %d", &address, &index);
sum += index;
arr[i] = sum;
mp[sum] = address;
}
int maxarr = 0;
for (int i = 0; i < k; i++) {
int a;
scanf("%d", &a);
int j;
for (j = 0; j < n; j++) {
if (a < arr[j]) {
if (j == 0) {
printf("%d\n", mp[arr[j]] + a * 4);
}
else {
printf("%d\n", mp[arr[j]] + (a - arr[j-1]) * 4);
}
if (maxarr < j) {
maxarr = j;
}
break;
}
}
if (j == n) {
printf("Illegal Access\n");
}
}
printf("%d", maxarr+1);
system("pause");
return 0;
}
7-2:扣帽子
#include<bits/stdc++.h>
using namespace std;
#pragma warning(disable:4996)
int n;
int hats[10001];
int hh[10001];
int arr[10001];
int aa[10001];
unordered_map<int, int> mp;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &hats[i]);
hh[i] = hats[i];
}
for (int i = 1; i <= n; i++) {
scanf("%d", &arr[i]);
aa[i] = arr[i];
}
sort(hh + 1, hh + 1 + n);
sort(aa + 1, aa + 1 + n);
for (int i = 1; i <= n; i++) {
mp[hh[i]] = aa[i];
}
bool flag = false;
for (int i = n; i > 0; i--) {
int paople = mp[hats[i]];
for (int j = 1; j <= n; j++) {
if (arr[j] == paople) {
if (flag)printf(" ");
else flag = true;
printf("%d", j);
}
}
}
system("pause");
return 0;
}
7-3:小朋友钻洞洞!
#include<bits/stdc++.h>
using namespace std;
#pragma warning(disable:4996)
int n, m;
vector<int> v[102];
int head[102];
int anshead, ansnum = 0;
vector<int> temp;
vector<int> ans;
bool visited[102];
void dfs(int cur) {
if (visited[cur]) return;
visited[cur] = true;
temp.push_back(cur);
for (auto each : v[cur]) {
if (!visited[each]) {
dfs(each);
}
}
if (temp.size() > ans.size()) {
ans = temp;
}
temp.pop_back();
}
int main() {
scanf("%d %d", &n, &m);
fill(head, head + 102, 0);
for (int i = 0; i < m; i++) {
int a, b;
scanf("%d %d", &a, &b);
v[a].push_back(b);
v[b].push_back(a);
head[a]++;
head[b]++;
}
for (int i = 1; i <= n; i++) {
sort(v[i].begin(), v[i].end());
}
bool flag = false;
for (int i = 1; i <= n; i++) {
flag = true;
temp.clear();
fill(visited, visited + 102, false);
dfs(i);
if (ans.size() > ansnum) {
anshead = i;
ansnum = ans.size();
}
}
printf("%d %d", anshead, ansnum);
system("pause");
return 0;
}
7-4:中序和最小堆建树!和某一年真题一样的解法!
#include<bits/stdc++.h>
using namespace std;
#pragma warning(disable:4996)
struct ppp {
int a;
int b;
};
struct node {
int id;
int val;
int priority;
node *left = nullptr, *right = nullptr;
};
ppp arr[32];
int n;
int in[32];
int hp[32];
node *root = nullptr;
void create(node *&r, int hl, int hr) {
if (hl > hr) return;
int minindex = -1;
for (int i = hl; i <= hr; i++) {
if (minindex == -1) minindex = i;
else {
if (hp[i] < hp[minindex]) {
minindex = i;
}
}
}
r = new node;
r->val = in[minindex];
r->priority = hp[minindex];
create(r->left, hl, minindex - 1);
create(r->right, minindex + 1, hr);
}
bool cmp(ppp &x, ppp &y) {
return x.a < y.a;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
int a, b;
scanf("%d %d", &a, &b);
arr[i].a = a;
arr[i].b = b;
}
sort(arr, arr + n, cmp);
for (int i = 0; i < n; i++) {
in[i] = arr[i].a;
hp[i] = arr[i].b;
}
create(root, 0, n-1);
//cout << root->val << " ";
int ans[32];
int pans[32];
int cnt = 0;
queue<node*> q;
q.push(root);
while (!q.empty()) {
node *temp = q.front();
q.pop();
ans[cnt] = temp->val;
pans[cnt] = temp->priority;
cnt++;
if (temp->left != nullptr) {
q.push(temp->left);
}
if (temp->right != nullptr) {
q.push(temp->right);
}
}
for (int i = 0; i < n; i++) {
if (i != 0) printf(" ");
printf("%d",ans[i]);
}
printf("\n");
for (int i = 0; i < n; i++) {
if (i != 0) printf(" ");
printf("%d", pans[i]);
}
system("pause");
return 0;
}
标签:pat2021,arr,temp,int,题解,scanf,++,甲级,printf 来源: https://blog.csdn.net/qq_37328912/article/details/120240504