19年PAT甲级春季题解

7-1 Sexy Primes

#include <cstdio>
#include <cmath>
using namespace std;

bool isPrime(int n){
    if(n<=1) return false;
    int sqr=(int)sqrt(1.0*n);
    for(int i=2;i<=sqr;i++)
        if(n%i==0) return false;
    return true;
}

bool isSexyPrime(int n){
    if(isPrime(n) && isPrime(n+6)) return true;
    else return false;
}

int main(void){
    int N;
    scanf("%d",&N);
    if(isSexyPrime(N-6)){
        printf("Yes\n%d",N-6);
    }else if(isSexyPrime(N)){
        printf("Yes\n%d",N+6);
    }else{
        printf("No\n");
        do{
            N++;
        }while(!isSexyPrime(N) && !isSexyPrime(N-6));
        printf("%d",N);
    }
    return 0;
}

在这里插入图片描述

7-2 Anniversary

#include <iostream>
#include <string>
#include <map>
using namespace std;
int main(){
	int n, m, cnt = 0;
	string ans, minn;
	map<string, int>mp;
	scanf("%d", &n);
	for(int i = 1; i <= n; ++ i){
		string temp;
		cin >> temp;
		mp[temp] = i;
	}
	scanf("%d", &m);
	for(int i = 0; i < m; ++ i){
		string temp, tempbirth;
		cin >> temp;
		tempbirth = temp.substr(6, 8);
		if(mp[temp] > 0){
			if(cnt ++ == 0 || tempbirth < minn){
				minn = tempbirth;
				ans = temp;
			}
		}
		if(cnt == 0){
			if(i == 0 || tempbirth < minn){
				minn = tempbirth;
				ans = temp;
			}
		}
	}
	printf("%d\n%s",cnt, ans.c_str());
}

在这里插入图片描述

7-3 Telefraud Detection

#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
using namespace std;

const int INF=0;
const int maxn=1030;
int N,M,K;
int G[maxn][maxn];
vector<int> suspect;
set<int> Set[maxn];
bool vis[maxn];
bool inq[maxn];

bool isSuspect(int i){
    int k=0,cnt=0,sum=0;
    for(int j=1;j<=N;j++){
        if(G[i][j]!=INF && G[i][j]<=5){
            k++;
            if(G[j][i]!=0) cnt++;
        }
    }
    if(k>K && 1.0*cnt/k<=0.2) return true;
    return false;
}

void BFS(int s){
    queue<int> q;
    q.push(s);
    inq[s]=true;
    while(!q.empty()){
        int u=q.front();
        q.pop();
        Set[s].insert(u);
        for(int i=0;i<=suspect.size();i++){
        	int v=suspect[i];
            if(!inq[v] && G[u][v]!=INF && G[v][u]!=INF){
            	q.push(v);
        		inq[u]=true;
			}
        }
    }
}

int main(void){
    fill(G[0],G[0]+maxn*maxn,INF);
    scanf("%d%d%d",&K,&N,&M);
    for(int i=0;i<M;i++){
        int caller,receiver,duration;
        scanf("%d%d%d",&caller,&receiver,&duration);
        G[caller][receiver]+=duration;
    }
    for(int i=1;i<=N;i++){
        if(isSuspect(i)){
            suspect.push_back(i);
        }
    }
    if(suspect.size()==0){
		printf("None");
		return 0;
	}
    for(int i=0;i<suspect.size();i++){
    	if(!inq[suspect[i]]){
    		BFS(suspect[i]);
		}
	}
	
	for(int i=0;i<suspect.size();i++){
		int v=suspect[i];
		if(vis[v]) continue;
		set<int>::iterator it=Set[v].begin();
		printf("%d",*it++);
		vis[*it]=true;
		for(;it!=Set[v].end();it++){
			printf(" %d",*it);
			vis[*it]=true;
		}
		printf("\n");
	}
    return 0;
}

在这里插入图片描述

7-4 Structure of a Binary Tree

#include <bits/stdc++.h>
using namespace std;
struct TreeNode{
    TreeNode *left, *right, *parent;
    int val;
    TreeNode(int v = 0):left(NULL), right(NULL), parent(NULL), val(v){}
};
int pos[32], ino[32];
map<int, TreeNode*> mp;
map<int, int> lev;
bool isfull = true;

TreeNode *build(int posL, int posR, int inoL, int inoR, int depth){
    if(posL > posR) return NULL;
    int e = pos[posR], idx = inoL;
    while(e != ino[idx]) ++idx;
    TreeNode *r = new TreeNode(e);
    lev[e] = depth;
    r->left = build(posL, posL + idx - 1 - inoL, inoL, idx - 1, depth + 1);
    r->right = build(posR - 1 -(inoR - idx - 1),posR - 1, idx + 1, inoR, depth + 1);
    if(r->left) r->left->parent = r;
    if(r->right) r->right->parent = r;
    if((!r->left && r->right) || (r->left && !r->right)) isfull = false;
    mp[e] = r;
    return r;
}
void sol(string &s, TreeNode *r){
    if(s.find("root") != string::npos){
        int num;
        sscanf(s.c_str(), "%d is the root", &num);
        r->val == num ? printf("Yes\n"):printf("No\n");
    }
    else if(s.find("siblings") != string::npos){
        int a, b;
        sscanf(s.c_str(), "%d and %d are siblings", &a, &b);
        TreeNode *p = mp[a], *q = mp[b];
        p->parent == q->parent ? printf("Yes\n") : printf("No\n");
    }
    else if(s.find("parent") != string::npos){
        int a, b;
        sscanf(s.c_str(), "%d is the parent of %d", &a, &b);
        TreeNode *p = mp[a], *q = mp[b];
        p == q->parent ? printf("Yes\n") : printf("No\n");
    }
    else if(s.find("left child") != string::npos){
        int a, b;
        sscanf(s.c_str(), "%d is the left child of %d", &a, &b);
        TreeNode *p = mp[a], *q = mp[b];
        p == q->left ? printf("Yes\n") : printf("No\n");
    }
    else if(s.find("right child") != string::npos){
        int a, b;
        sscanf(s.c_str(), "%d is the right child of %d", &a, &b);
        TreeNode *p = mp[a], *q = mp[b];
        p == q->right ? printf("Yes\n") : printf("No\n");
    }
    else if(s.find("level") != string::npos){
        int a, b;
        sscanf(s.c_str(), "%d and %d are on the same level", &a, &b);
        lev[a] == lev[b] ? printf("Yes\n") : printf("No\n");
    }
    else if(s.find("full") != string::npos){
        isfull ? printf("Yes\n") : printf("No\n");
    }
}
int main()
{
    int n, m;
    scanf("%d", &n);
    for(int i = 0; i < n; ++i){
        scanf("%d", pos + i);
    }
    for(int i = 0; i < n; ++i){
        scanf("%d", ino + i);
    }
    TreeNode *r = build(0, n - 1, 0, n - 1, 1);
    scanf("%d", &m);
    getchar();
    while(m--){
        string s;
        getline(cin, s);
        sol(s, r);
    }
    return 0;
}

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值