PAT(甲级)2017年春季考试
PAT(甲级)2017年春季考试
A.Raffle for Weibo Followers
#include<bits/stdc++.h>
using namespace std;
int m,n,s;
vector<string> person;
set<string> se;
vector<string> ans;
int main(){
cin>>m>>n>>s;
for(int i=1;i<=m;i++){
string name;
cin>>name;
person.push_back(name);
}
int num = person.size();
// for(int i=0;i<num;i++) cout<<person[i]<<endl;
if(s > num) puts("Keep going...");
else{
int pos = s-1;
while(pos < num){
if(se.find(person[pos]) == se.end()){
se.insert(person[pos]);
// cout<<pos<<endl;
ans.push_back(person[pos]);
pos = pos + n;
}else{
pos++;
}
}
for(int i=0;i<ans.size();i++) cout<<ans[i]<<endl;
}
return 0;
}
B.Chain the Ropes优先队列
#include<bits/stdc++.h>
using namespace std;
priority_queue<int,vector<int>,greater<int> > que;
int n;
int main(){
cin>>n;
for(int i=1;i<=n;i++){
int d;
cin>>d;
que.push(d);
}
while(que.size() != 1){
int fir = que.top();
que.pop();
int sec = que.top();
que.pop();
int thir = (fir + sec)/2;
que.push(thir);
}
cout<<que.top();
return 0;
}
C.Eulerian Path
还没做,统计度数来判断是不是欧拉回路,dfs判断是否连通图
#include<bits/stdc++.h>
using namespace std;
const int maxn = 520;
int n,m;
vector<int> g[maxn];
int degree[maxn];
int odd = 0,even = 0;
bool vis[maxn];
int cnt = 0;
void dfs(int x){
vis[x] = true;
cnt++;
for(int i=0;i<g[x].size();i++){
if(vis[g[x][i]] == false) dfs(g[x][i]);
}
}
int main(){
cin>>n>>m;
for(int i=1;i<=m;i++){
int u,v;
cin>>u>>v;
g[u].push_back(v);
g[v].push_back(u);
degree[u]++;
degree[v]++;
}
for(int i=1;i<=n;i++){
if(degree[i]%2 == 0) even++;
else odd++;
}
dfs(1);
if(n == 0) {
puts("Non-Eulerian");
return 0;
}
if(n >= 1) cout<<degree[1];
for(int i=2;i<=n;i++) cout<<" "<<degree[i];
if(n >= 1) cout<<endl;
if(even == n && cnt == n) puts("Eulerian");
else if(odd == 2 && cnt == n) puts("Semi-Eulerian");
else puts("Non-Eulerian");
return 0;
}
D.ZigZagging on a Tree
二叉树,中序后序建树,输出Z字型层次遍历的结果
#include<bits/stdc++.h>
using namespace std;
const int maxn = 50;
int n;
int post[maxn];
int in[maxn];
vector<int> ans[maxn];
struct node{
int v;
node *l;
node *r;
};
int maxDepth = 0;
void dfs(node *root,int depth){
if(depth > maxDepth) maxDepth = depth;
ans[depth].push_back(root->v);
if(root->l) dfs(root->l,depth+1);
if(root->r) dfs(root->r,depth+1);
}
node * build(int root,int il,int ir) {
if (il > ir) return NULL;
int pos = il;
while (pos <= ir && in[pos] != post[root])
pos++;
node* Root = new node;
Root->v = post[root];
Root->r = build(root-1, pos+1,ir );
Root->l = build(root-(ir-pos)-1 ,il,pos - 1);
return Root;
}
int main(){
cin>>n;
for(int i=1;i<=n;i++) cin>>in[i];
for(int i=1;i<=n;i++) cin>>post[i];
node *Root = new node();
Root = build(n,1,n);
dfs(Root,1);
cout<<ans[1][0];
for(int i=2;i<=maxDepth;i++){
if(i%2==0){
for(int j=0;j<ans[i].size();j++){
cout<<" "<<ans[i][j];
}
}else{
for(int j=ans[i].size()-1;j>=0;j--){
cout<<" "<<ans[i][j];
}
}
}
return 0;
}