PAT(甲级)2019年春季考试
7-1 Sexy Primes 判断素数 一个点没过17/20分
错因:输出i-6写成了输出i,当时写的很乱,可以参考其他人的写法
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e8;
typedef long long ll;
int n;
bool isprime(ll x){
if(x == 0 || x==1 || x==2) return false;
for(ll i=2;i<=sqrt(x);i++){
if(x%i==0) return false;
}
return true;
}
int main(){
scanf("%lld",&n);
if(isprime(n)){
if(n>=6 && isprime(n-6)){
puts("Yes");
printf("%lld",n-6);
}else if(isprime(n+6)){
puts("Yes");
printf("%lld",n+6);
}else{
puts("No");
for(ll i=n+1;i<=maxn;i++){
if(i-6>=0){
if((i-6)>=0 && isprime(i) && isprime(i-6)){
printf("%lld",i);
break;
}
}else{
if(isprime(i) && isprime(i+6)){
printf("%lld",i);
break;
}
}
}
}
}else{
puts("No");
for(ll i=n+1;i<=maxn;i++){
if(i-6>=0){
if((i-6)>=0 && isprime(i) && isprime(i-6)){
printf("%lld",i);
break;
}
}else{
if(isprime(i) && isprime(i+6)){
printf("%lld",i);
break;
}
}
}
}
return 0;
}
修改后的代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e8+10;
typedef long long ll;
int n;
bool isprime(ll x){
if(x == 0 || x==1 || x==2) return false;
for(ll i=2;i<=sqrt(x);i++){
if(x%i==0) return false;
}
return true;
}
int main(){
scanf("%lld",&n);
printf("i = %d\n",ac);
if(isprime(n)){
if(n>=6 && isprime(n-6)){
puts("Yes");
printf("%lld",n-6);
}else if(isprime(n+6)){
puts("Yes");
printf("%lld",n+6);
}else{
puts("No");
for(ll i=n+1;i<=maxn;i++){
if(i-6>=0){
if((i-6)>=0 && isprime(i) && isprime(i-6)){
printf("%lld",i);
break;
}
}else{
if(isprime(i) && isprime(i+6)){
printf("%lld",i);
break;
}
}
}
}
}else{
puts("No");
for(ll i=n+1;i<=maxn;i++){
if(i-6>=0){
if((i-6)>=0 && isprime(i) && isprime(i-6)){
printf("%lld",i - 6);
break;
}else if(isprime(i) && isprime(i+6)){
printf("%lld",i);
break;
}
}else{
if(isprime(i) && isprime(i+6)){
printf("%lld",i);
break;
}
}
}
}
return 0;
}
7-2 Anniversary 集合、查找排序 25分
#include<bits/stdc++.h>
using namespace std;
set<string> se1;
set<string> se2;
int n,m;
int main(){
cin>>n;
for(int i=1;i<=n;i++){
string id;
cin>>id;
se1.insert(id);
}
int ans = 0;
string guestold = "99999999";
string assold = "99999999";
string guest;
string ass;
cin>>m;
for(int i=1;i<=m;i++){
string id;
cin>>id;
se2.insert(id);
string temp;
for(int j=6;j<=13;j++) temp.push_back(id[j]);
if(se1.find(id) != se1.end()){
ans++;
if(assold > temp) {
assold = temp;
ass = id;
}
}
if(guestold > temp) {
guestold = temp;
guest = id;
}
}
cout<<ans<<endl;
if(ans != 0){
if(n !=0){
cout<<ass<<endl;
}
}else{
if(m!=0){
cout<<guest<<endl;
}
}
return 0;
}
7-3 Telefraud Detection 图论 15/25分
更新错误原因:没有完全搞清题意,total duration,表示两人之间累计的通话时间
下面的代码只算了最后1次时间(覆盖)
修改后,可以用并查集做,不过感觉没必要,统计入度出度+暴力判断集合也不会超时的
参考链接
#include<bits/stdc++.h>
using namespace std;
/*
邻接矩阵
点:出度 和他的边相关的入度
*/
const int maxn = 1010;
int k,n,m;
int g[maxn][maxn];
vector<int> sss;
int vis[maxn];
vector<int> temp;
set<int> se;
void print(){
if(temp.size()==0) return;
cout<<temp[0];
for(int i=1;i<temp.size();i++){
cout<<" "<<temp[i];
}
cout<<endl;
}
int main(){
cin>>k>>n>>m;
for(int i=1;i<=m;i++){
int u,v,w;
cin>>u>>v>>w;
g[u][v] = w;
}
for(int i=1;i<=n;i++){
int call = 0;
int back = 0;
for(int j=1;j<=n;j++){
if(g[i][j]!=0 && g[i][j] <= 5){
call++;
if(g[j][i] != 0) back++;
}
}
if(call > k && back*5 <= call){
sss.push_back(i);
se.insert(i);
}
}
if(sss.size() == 0){
cout<<"None"<<endl;
return 0;
}
sort(sss.begin(),sss.end());
for(int i=0;i<sss.size();i++){
int x = sss[i];
temp.clear();
if(!vis[x]){
vis[x] = 1;
temp.push_back(x);
for(int j=i+1;j<sss.size();j++){
//判断第sss[i]能否可以用
bool can = true;
for(int k=0;k<temp.size();k++){
if(g[sss[j]][temp[k]] == 0){
can = false;
break;
}
}
if(can == true){
vis[sss[j]] = 1;
temp.push_back(sss[j]);
}
}
print();
}
}
return 0;
}
7-4 Structure of a Binary Tree 二叉树中序后序建树,dfs深搜30分
#include<bits/stdc++.h>
using namespace std;
int n,m;
const int maxn = 1100;
int in[maxn];
int post[maxn];
struct node{
int v;
node* l;
node* r;
};
int level[maxn];
int l[maxn];
int r[maxn];
int sib[maxn];
int par[maxn];
bool full = true;
void dfs(node *root,int depth){
if(root == NULL) return;
int x = root->v;
level[x] = depth;
if(root->l != NULL){
par[root->l->v] = x;
l[x] = root->l->v;
dfs(root->l,depth+1);
}
if(root->r != NULL){
par[root->r->v] = x;
r[x] = root->r->v;
dfs(root->r,depth+1);
}
if(root->l != NULL && root->r != NULL){
sib[root->l->v] = root->r->v;
sib[root->r->v] = root->l->v;
}
if((root->l == NULL && root->r !=NULL) || (root->r == NULL && root->l !=NULL) ){
full = false;
}
}
node* build(int il,int ir,int pl,int pr){
if(il > ir) return NULL;
int rootv = post[pr];
int pos = il;
while(pos <= ir && in[pos] != rootv) pos++;
node *root = new node;
root->v = rootv;
root->l = build(il,pos-1,pl,pr-(ir-pos)-1);
// pr-(ir-pos)+1
// root->r = build(pos+1,ir,pl+(pos-il)+1,pr);
root->r = build(pos+1,ir,pr-(ir-pos)+1,pr-1);
return root;
}
int main(){
cin>>n;
for(int i=1;i<=n;i++) cin>>post[i];
for(int j=1;j<=n;j++) cin>>in[j];
node* Root = build(1,n,1,n);
dfs(Root,1);
cin>>m;
getchar();
while(m--){
string stat;
getline(cin, stat);
if(stat.find("root") != string::npos){
int root = 0;
for(int i=0;i<stat.length();i++){
if(stat[i] > '9' || stat[i] < '0') break;
root = root*10 + (stat[i] - '0');
}
if(root == Root->v){
puts("Yes");
}else{
puts("No");
}
}else if(stat.find("siblings") != string::npos){
int parent = 0;
int pos = 0;
for(int i=0;i<stat.length();i++){
if(stat[i] > '9' || stat[i] < '0') break;
parent = parent*10 + (stat[i] - '0');
pos = i;
}
int child = 0;
for(int i = pos+1;i<stat.length();i++){
if(stat[i] > '9' || stat[i] < '0') continue;
child = child*10 + (stat[i] - '0');
}
if(sib[parent] == child && sib[child] == parent){
puts("Yes");
}else{
puts("No");
}
}else if(stat.find("parent") != string::npos){
int parent = 0;
int pos = 0;
for(int i=0;i<stat.length();i++){
if(stat[i] > '9' || stat[i] < '0') break;
parent = parent*10 + (stat[i] - '0');
pos = i;
}
int child = 0;
for(int i = pos+1;i<stat.length();i++){
if(stat[i] > '9' || stat[i] < '0') continue;
child = child*10 + (stat[i] - '0');
}
if(par[child] == parent){
puts("Yes");
}else{
puts("No");
}
}else if(stat.find("left") != string::npos){
int parent = 0;
int pos = 0;
for(int i=0;i<stat.length();i++){
if(stat[i] > '9' || stat[i] < '0') break;
parent = parent*10 + (stat[i] - '0');
pos = i;
}
int child = 0;
for(int i = pos+1;i<stat.length();i++){
if(stat[i] > '9' || stat[i] < '0') continue;
child = child*10 + (stat[i] - '0');
}
if(l[child] == parent){
puts("Yes");
}else{
puts("No");
}
}else if(stat.find("right") != string::npos){
int parent = 0;
int pos = 0;
for(int i=0;i<stat.length();i++){
if(stat[i] > '9' || stat[i] < '0') break;
parent = parent*10 + (stat[i] - '0');
pos = i;
}
int child = 0;
for(int i = pos+1;i<stat.length();i++){
if(stat[i] > '9' || stat[i] < '0') continue;
child = child*10 + (stat[i] - '0');
}
if(r[child] == parent){
puts("Yes");
}else{
puts("No");
}
}else if(stat.find("same") != string::npos){
int parent = 0;
int pos = 0;
for(int i=0;i<stat.length();i++){
if(stat[i] > '9' || stat[i] < '0') break;
parent = parent*10 + (stat[i] - '0');
pos = i;
}
int child = 0;
for(int i = pos+1;i<stat.length();i++){
if(stat[i] > '9' || stat[i] < '0') continue;
child = child*10 + (stat[i] - '0');
}
if(level[parent] == level[child]){
puts("Yes");
}else{
puts("No");
}
}else if(stat.find("full") != string::npos){
if(full) puts("Yes");
else puts("No");
}
}
return 0;
}