为了准备这次甲级,把最近两年的题目都花钱做了一遍。从后往前做的,感觉就是越来越容易了,所以==,PAT考试是越来越难了??明天就考试了,阿门祝我好运。
还有最近喜欢上了金韩彬,妈呀,好久没这么喜欢一个明星了,好开心。过几天专辑应该就到了,开心!幸福!
7-1 Sexy Primes (20 分)
质数
#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
int n;
bool is_prime(int x){
if(x<=1) return false;
int t = sqrt(x);
for(int i = 2; i <= t; i++)
if(x%i==0) return false;
return true;
}
bool judge(int x){
if(!is_prime(x)) return false;
if(is_prime(x-6) || is_prime(x+6)) return true;
return false;
}
int main(){
scanf("%d", &n);
if(judge(n)){
printf("Yes\n");
if(is_prime(n-6)) printf("%d\n", n-6);
else printf("%d\n", n+6);
return 0;
}
printf("No\n");
int t = n+1;
while(!judge(t)) t++;
printf("%d\n", t);
}
7-2 Anniversary (25 分)
#include<iostream>
#include<vector>
#include<unordered_set>
using namespace std;
int N, M;
int main(){
scanf("%d", &N);
unordered_set<string> ids;
for(int i = 0; i < N; i++){
string s; cin >> s;
ids.insert(s);
}
scanf("%d", &M);
string s1, s2; int cnt = 0;
for(int i = 0; i < M; i++){
string s; cin >> s;
if(ids.count(s)){
cnt++;
if(s1.empty()) s1 = s;
else {
string x = s1.substr(6, 8), y = s.substr(6, 8);
if(x > y) s1 = s;
}
}
if(s2.empty()) s2 = s;
else {
string x = s2.substr(6, 8), y = s.substr(6, 8);
if(x > y) s2 = s;}
}
printf("%d\n", cnt);
if(cnt==0) cout << s2 << endl;
else cout << s1 << endl;
}
7-3 Telefraud Detection (25 分)
#include<iostream>
#include<vector>
#include<unordered_map>
#include<algorithm>
using namespace std;
int K, N, M;
unordered_map<int, int> mp;
int find_f(int x){
if(mp.count(x)==0 || mp[x]==x) {mp[x] = x; return mp[x];}
mp[x] = find_f(mp[x]);
return mp[x];
}
void handle(int x, int y){
int u = find_f(x), v = find_f(y);
mp[u] = v;
}
int main(){
scanf("%d %d %d", &K, &N, &M);
vector<vector<int>> grid(N+1, vector<int>(N+1, 0));
for(int i = 0; i < M; i++){
int x, y, z; scanf("%d %d %d", &x, &y, &z);
grid[x][y] += z;
}
vector<int> cnt1(N+1, 0); vector<int> cnt2(N+1, 0);
vector<int> ans;
for(int i = 1; i <= N; i++){
for(int j = 1; j <= N; j++){
if(grid[i][j]>0 && grid[i][j]<=5){
cnt1[i]++;
if(grid[j][i]!=0) cnt2[i]++;
}
}
if(cnt1[i]>K && 5*cnt2[i]<=cnt1[i]) ans.push_back(i);
}
if(ans.empty()) {printf("None\n"); return 0;}
//for(auto a:ans) cout << a << " "; cout << endl;
for(int i = 0; i < ans.size(); i++){
for(int j = i+1; j < ans.size(); j++){
if(grid[ans[i]][ans[j]]!=0 && grid[ans[j]][ans[i]]!=0) handle(ans[i], ans[j]);
}
}
unordered_map<int, vector<int>> flags;
for(auto a:ans){
int t = find_f(a);
flags[t].push_back(a);
}
vector<vector<int>> Ans;
for(auto &a:flags) {
sort(a.second.begin(), a.second.end());
Ans.push_back(a.second);
}
sort(Ans.begin(), Ans.end());
for(auto a:Ans){
bool first = true;
for(int i = 0; i < a.size(); i++){
if(first) first = false; else printf(" ");
printf("%d", a[i]);
}
printf("\n");
}
}
7-4 Structure of a Binary Tree (30 分)
#include<iostream>
#include<vector>
#include<unordered_map>
#include<sstream>
#include<cctype>
using namespace std;
int N, M;
vector<int> postorder, inorder;
unordered_map<int, int> pos;
unordered_map<int, int> Left, Right;
unordered_map<int, int> parent;
unordered_map<int, int> Level;
int build(int l1, int r1, int l2, int r2){
if(l1>r1) return -1;
int t = postorder[r1]; int p = pos[t];
int l = build(l1, l1+p-1-l2, l2, p-1);
int r = build(r1+p-r2, r1-1, p+1, r2);
Left[t] = l; Right[t] = r; parent[l] = t; parent[r] = t;
return t;
}
void debug(int x){
if(x==-1) return;
cout << x << " ";
debug(Left[x]);
debug(Right[x]);
}
bool is_full = true;
void judge_full(int x){
if(x==-1) return;
if((Left[x]==-1 && Right[x]!=-1) || (Left[x]!=-1 && Right[x]==-1)) is_full = false;
judge_full(Left[x]);
judge_full(Right[x]);
}
int deep = 0;
void handle_level(int x){
if(x==-1) return;
Level[x] = deep;
deep++;
handle_level(Left[x]); handle_level(Right[x]);
deep--;
}
int main(){
scanf("%d", &N);
for(int i = 0; i < N; i++) {int x; scanf("%d", &x); postorder.push_back(x);}
for(int i = 0; i < N; i++) {int x; scanf("%d", &x); inorder.push_back(x); pos[x] = i;}
int h = build(0, N-1, 0, N-1); judge_full(h); handle_level(h);
//debug(h);
scanf("%d", &M);
string s; getline(cin, s);
for(int i = 0; i < M; i++){
getline(cin, s);
stringstream ss; ss<<s;
string word;
ss>>word;
if(!isdigit(word[0])) {
if(is_full) printf("Yes\n");
else printf("No\n");
continue;
}
int x = atoi(word.c_str());
ss >> word;
if(word=="is"){
ss >> word; ss >> word;
if(word=="root") {if(x==h) printf("Yes\n"); else printf("No\n"); continue;}
if(word=="parent") {ss >> word; ss >> word; int y = atoi(word.c_str());
if(Left[x]==y || Right[x]==y) printf("Yes\n"); else printf("No\n"); continue;}
if(word=="left") {ss>>word; ss >> word; ss>>word; int y = atoi(word.c_str());
if(Left[y]==x) printf("Yes\n"); else printf("No\n"); continue;}
if(word=="right") {ss>>word; ss>>word; ss >> word; int y = atoi(word.c_str());
if(Right[y]==x) printf("Yes\n"); else printf("No\n"); continue;}
}
ss >> word; int y = atoi(word.c_str());
ss >> word; ss >> word;
if(word == "siblings") {
if(parent[x]==parent[y]) printf("Yes\n"); else printf("No\n");
continue;
}
if(Level[x]==Level[y]) printf("Yes\n"); else printf("No\n");
}
}