PAT A1124~A1127
作者:互联网
一、PAT A1124 Raffle for Weibo Followers
- 简单的模拟,细心一点就好啦。参考代码如下。
#include<iostream>
#include<string>
#include<unordered_map>
using namespace std;
unordered_map <string, bool> is_choose;
int main() {
int n, m, s;
string name;
cin >> n >> m >> s;
bool have = false;
for (int i = 1, t = s; i <= n; i++) {
cin >> name;
if (i == t) {
if (!is_choose[name]) {
cout << name << endl;
t += m;
is_choose[name] = true;
have = true;
}
else t++;
}
}
if (!have)
cout << "Keep going..." << endl;
return 0;
}
二、PAT A1125 Chain the Ropes
- 显然长绳子的折叠次数越少越好,所以应把长绳子放在最后叠,所以此题即为堆果子问题,利用优先级队列即可解决。注意最后的输出。参考代码如下。
#include<cstdio>
#include<vector>
#include<queue>
#include<math.h>
using namespace std;
priority_queue <float, vector<float>, greater<float>> pq;
int main() {
int n;
float t, a, b;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%f", &t);
pq.push(t);
}
for (int i = 1; i < n; i++) {
a = pq.top(); pq.pop();
b = pq.top(); pq.pop();
pq.push((a + b) / 2.0);
}
printf("%.0f\n", floor(pq.top()));
return 0;
}
三、PAT A1126 Eulerian Path
- 首先需要判断图是否连通,这里用 dfs 进行一次遍历,如果有未访问到的点说明图不连通。如果是连通图,则要考虑顶点的度的奇偶。如果恰有两个点的度为奇数,则是 Semi-Eulerian,如果顶点的度数全为偶数则是 Eulerian,否则即为 Non-Eulerian。参考代码如下。
#include<iostream>
#include<vector>
using namespace std;
const int maxn = 505;
vector <int> adj[maxn];
bool vis[maxn];
void dfs(int r) {
vis[r] = true;
for (auto it : adj[r])
if (!vis[it]) dfs(it);
}
int main() {
int n, m, u, v, cnt = 0;
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1);
for (int i = 1; i <= n; i++) {
if (i != 1) cout << ' ';
cout << adj[i].size();
if (adj[i].size() % 2 == 1) cnt++;
}
cout << endl;
for (int i = 1; i <= n; i++)
if (!vis[i]) {
cout << "Non-Eulerian" << endl;
return 0;
}
if (cnt == 0) cout << "Eulerian" << endl;
else if (cnt == 2) cout << "Semi-Eulerian" << endl;
else cout << "Non-Eulerian" << endl;
return 0;
}
四、PAT A1127 ZigZagging on a Tree
- 先建树,然后层次遍历,同时记录每个结点的所在层次,存储层次遍历的结果。输出时将层次为偶数的一连串结点正序输出,将层次为奇数的一连串结点逆序输出,逆序输出时使用了栈。参考代码如下。
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
struct node {
int v;
int level;
node* left = NULL;
node* right = NULL;
};
int in[50], post[50];
vector <node*> ans;
node* build(int inL, int inR, int postL, int postR) {
if (inL > inR) return NULL;
node* root = new node;
root->v = post[postR];
int k;
for (k = inR; k >= inL; k--)
if (in[k] == post[postR]) break;
int num = inR - k;
root->left = build(inL, k - 1, postL, postR - num - 1);
root->right = build(k + 1, inR, postR - num, postR - 1);
return root;
}
void level(node* root) {
queue <node*> q;
root->level = 1;
q.push(root);
ans.push_back(root);
while (q.size()) {
node* temp = q.front();
q.pop();
if (temp->left) {
temp->left->level = temp->level + 1;
q.push(temp->left);
ans.push_back(temp->left);
}
if (temp->right) {
temp->right->level = temp->level + 1;
q.push(temp->right);
ans.push_back(temp->right);
}
}
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> in[i];
for (int i = 0; i < n; i++) cin >> post[i];
node* root = build(0, n - 1, 0, n - 1);
level(root);
cout << ans[0]->v;
int i = 1;
while (i < ans.size()) {
if (ans[i]->level % 2 == 0)
cout << ' ' << ans[i++]->v;
else {
stack <node*> s;
while (i < ans.size() && ans[i]->level % 2 == 1)
s.push(ans[i++]);
while (s.size()) {
cout << ' ' << s.top()->v;
s.pop();
}
}
}
return 0;
}
标签:PAT,temp,A1127,int,level,push,A1124,include,root 来源: https://blog.csdn.net/weixin_42717165/article/details/88052651