2019春季PAT甲级题解

7-1 Sexy Prime

题目:
Sexy primes are pairs of primes of the form (p, p+6), so-named since “sex” is the Latin word for “six”. (Quoted from http://mathworld.wolfram.com/SexyPrimes.html)

Now given an integer, you are supposed to tell if it is a sexy prime.

Input Specification:
Each input file contains one test case. Each case gives a positive integer N (≤10^8).

Output Specification:
For each case, print in a line Yes if N is a sexy prime, then print in the next line the other sexy prime paired with N (if the answer is not unique, output the smaller number). Or if N is not a sexy prime, print No instead, then print in the next line the smallest sexy prime which is larger than N.

Sample Input 1:
47
Sample Output 1:
Yes
41
Sample Input 2:
21
Sample Output 2:
No
23

题目大意:
很简单,判断(a,a-6)均为素数,若是则输出Yes和a-6;否则判断(a,a+6),满足则输出Yes和a+6;若上述情况均不满足,则去寻找大于a的最小满足Sexy Prime条件的b,并输出No和b。

AC代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n;
bool isprime(ll a)
{
    if(a<=1) return false;
    ll sqr=(ll)sqrt(1.0*a);
    for(ll i=2;i<=sqr;i++) if(a%i==0) return false;
    return true;
}
ll sexyprime(ll a)
{
    if(isprime(a)&&isprime(a-6)) return a-6;
    if(isprime(a)&&isprime(a+6)) return a+6;
    return -1;
}
int main()
{
    cin>>n;
    if(sexyprime(n)!=-1) { printf("Yes\n%lld",sexyprime(n)); return 0; }
    while(sexyprime(n)==-1) n++;
    printf("No\n%lld",n);
    return 0;
}

7-2 Anniversay

题目:
Zhejiang University is about to celebrate her 122th anniversary in 2019. To prepare for the celebration, the alumni association (校友会) has gathered the ID’s of all her alumni. Now your job is to write a program to count the number of alumni among all the people who come to the celebration.

Input Specification:
Each input file contains one test case. For each case, the first part is about the information of all the alumni. Given in the first line is a positive integer N (≤10^5​ ). Then N lines follow, each contains an ID number of an alumnus. An ID number is a string of 18 digits or the letter X. It is guaranteed that all the ID’s are distinct.

The next part gives the information of all the people who come to the celebration. Again given in the first line is a positive integer M (≤10^​5​​ ). Then M lines follow, each contains an ID number of a guest. It is guaranteed that all the ID’s are distinct.

Output Specification:
First print in a line the number of alumni among all the people who come to the celebration. Then in the second line, print the ID of the oldest alumnus – notice that the 7th - 14th digits of the ID gives one’s birth date. If no alumnus comes, output the ID of the oldest guest instead. It is guaranteed that such an alumnus or guest is unique.

Sample Input:
5
372928196906118710
610481197806202213
440684198612150417
13072819571002001X
150702193604190912
6
530125197901260019
150702193604190912
220221196701020034
610481197806202213
440684198612150417
370205198709275042
Sample Output:
3
150702193604190912

题目大意:
略,等空了再上题目解释,代码先上。。。。。
AC代码:

#include <bits/stdc++.h>
using namespace std;
struct node{
    string id;
    string yy,mm,dd;
}aln[100001];
map<string,int> mp;
int n,m;
int main()
{
    cin>>n;
    string str,oldest="999999999999999999";
    for(int i=0;i<n;i++)
    {
        cin>>str;
        mp[str]=i;
        aln[i].id=str;
        aln[i].yy=str.substr(6,4);
        aln[i].mm=str.substr(10,2);
        aln[i].dd=str.substr(12,2);
    }
    cin>>m;
    int ans=0;
    while(m--)
    {
        cin>>str;
        if(mp.find(str)!=mp.end())
        {
            if(ans==0) oldest="999999999999999999";
            ans++;
            if(oldest.substr(6,4)>aln[mp[str]].yy) oldest=str;
            else if(oldest.substr(6,4)==aln[mp[str]].yy)
            {
                if(oldest.substr(10,2)>aln[mp[str]].mm) oldest=str;
                else if(oldest.substr(10,2)==aln[mp[str]].mm)
                {
                    if(oldest.substr(12,2)>aln[mp[str]].dd) oldest=str; 
                }
            }
        }
        else if(ans==0)
        {
            if(oldest.substr(6,4)>str.substr(6,4)) oldest=str;
            else if(oldest.substr(6,4)==str.substr(6,4))
            {
                if(oldest.substr(10,2)>str.substr(10,2)) oldest=str;
                else if(oldest.substr(10,2)==str.substr(10,2))
                {
                    if(oldest.substr(12,2)>str.substr(12,2)) oldest=str; 
                }
            }
        }
    }
    printf("%d\n%s",ans,oldest.c_str());
    return 0;
}

7-3 Telefraud Detection

题目:
Telefraud(电信诈骗) remains a common and persistent problem in our society. In some cases, unsuspecting victims lose their entire life savings. To stop this crime, you are supposed to write a program to detect those suspects from a huge amount of phone call records.

A person must be detected as a suspect if he/she makes more than K short phone calls to different people everyday, but no more than 20% of these people would call back. And more, if two suspects are calling each other, we say they might belong to the same gang. A makes a short phone call to B means that the total duration of the calls from A to B is no more than 5 minutes.

Input Specification:
Each input file contains one test case. For each case, the first line gives 3 positive integers K (≤500, the threshold(阈值) of the amount of short phone calls), N (≤10^​3, the number of different phone numbers), and M (≤10 ^​5​​ , the number of phone call records). Then M lines of one day’s records are given, each in the format:

caller receiver duration
where caller and receiver are numbered from 1 to N, and duration is no more than 1440 minutes in a day.

Output Specification:
Print in each line all the detected suspects in a gang, in ascending order of their numbers. The gangs are printed in ascending order of their first members. The numbers in a line must be separated by exactly 1 space, and there must be no extra space at the beginning or the end of the line.

If no one is detected, output None instead.

Sample Input 1:
5 15 31
1 4 2
1 5 2
1 5 4
1 7 5
1 8 3
1 9 1
1 6 5
1 15 2
1 15 5
3 2 2
3 5 15
3 13 1
3 12 1
3 14 1
3 10 2
3 11 5
5 2 1
5 3 10
5 1 1
5 7 2
5 6 1
5 13 4
5 15 1
11 10 5
12 14 1
6 1 1
6 9 2
6 10 5
6 11 2
6 12 1
6 13 1
Sample Output 1:
3 5
6
Note: In sample 1, although 1 had 9 records, but there were 7 distinct receivers, among which 5 and 15 both had conversations lasted more than 5 minutes in total. Hence 1 had made 5 short phone calls and didn’t exceed the threshold 5, and therefore is not a suspect.

Sample Input 2:
5 7 8
1 2 1
1 3 1
1 4 1
1 5 1
1 6 1
1 7 1
2 1 1
3 1 1
Sample Output 2:
None

题目大意:
这道题与A1034很像,解题思路和题意都很像,但是我第一次做居然没做出来,吃了晚饭来重写一遍居然一次性AC了,有点气人。。。。。。

AC代码:

#include <bits/stdc++.h>
using namespace std;
const int maxn=1001;
int call[maxn][maxn];   //call[a][b]记录a给b打电话的时间总数
vector<int> suspect;
set<int> ans[maxn];
int k,n,m,father[maxn];
int findfather(int x)
{
    int a=x;
    while(x!=father[x]) x=father[x];
    while(a!=father[a])  //可以不用压缩路径
    {
        int z=a;
        a=father[a];
        father[z]=x;
    }
    return x;
}
void merge(int a,int b)
{
    int fa=findfather(a);
    int fb=findfather(b);
    if(fa<fb) father[fb]=fa;      //把数值较小的当做父结点
    else if(fb<fa) father[fa]=fb; 
}
int main()
{
    int a,b,w;
    cin>>k>>n>>m;
    for(int i=0;i<maxn;i++) father[i]=i;
    for(int i=0;i<m;i++)
    {
        cin>>a>>b>>w;
        call[a][b]+=w;
    }
    for(int i=1;i<=n;i++)
    {
        int cnt=0,num=0;
        for(int j=1;j<=n;j++)
        {
            if(call[i][j]>0&&call[i][j]<=5)
            {
                cnt++;
                if(call[j][i]!=0) num++;
            }
        }
        if(cnt>k&&1.0*(num)/cnt<=0.2) suspect.push_back(i);
    }
    for(int i=0;i<suspect.size();i++)
    {
        for(int j=i+1;j<suspect.size();j++)
        {
            if(call[suspect[i]][suspect[j]]!=0&&call[suspect[j]][suspect[i]]!=0) merge(suspect[i],suspect[j]);
        }
    }
    for(int i=0;i<suspect.size();i++)
    {
        int a=findfather(suspect[i]);
        ans[a].insert(suspect[i]);
    }
    for(int i=0;i<suspect.size();i++)
    {
        for(auto it=ans[suspect[i]].begin();it!=ans[suspect[i]].end();it++)
        {
            if(it!=ans[suspect[i]].begin()) printf(" ");
            printf("%d",*it);
        }
        if(ans[suspect[i]].size()!=0) printf("\n");
    }
    if(suspect.size()==0) printf("None");
    return 0;
}

7-4 Structure of a Binary Tree

题目:
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, a binary tree can be uniquely determined.

Now given a sequence of statements about the structure of the resulting tree, you are supposed to tell if they are correct or not. A statment is one of the following:

A is the root
A and B are siblings
A is the parent of B
A is the left child of B
A is the right child of B
A and B are on the same level
It is a full tree

Note:
Two nodes are on the same level, means that they have the same depth.
A full binary tree is a tree in which every node other than the leaves has two children.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤30), the total number of nodes in the binary tree. The second line gives the postorder sequence and the third line gives the inorder sequence. All the numbers in a line are no more than 10^​3 and are separated by a space.

Then another positive integer M (≤30) is given, followed by M lines of statements. It is guaranteed that both A and B in the statements are in the tree.

Output Specification:
For each statement, print in a line Yes if it is correct, or No if not.

Sample Input:
9
16 7 11 32 28 2 23 8 15
16 23 7 32 11 2 28 15 8
7
15 is the root
8 and 2 are siblings
32 is the parent of 11
23 is the left child of 16
28 is the right child of 2
7 and 11 are on the same level
It is a full tree
Sample Output:
Yes
No
Yes
No
Yes
Yes
Yes

题目大意:
题目并不难理解,难在字符串的处理很繁复,但思路还是比较清晰的,先建树,在遍历二叉树按题中state把需要的信息保存下来,最后按查询字符串一个一个检验。

AC代码:

#include <bits/stdc++.h>
using namespace std;
int post[32],ino[32];
struct node{
    int data;
    node *l,*r;
};
struct Node{
    int parent;
    int lc,rc;
    int level;
}stu[1001];
int n,k;
bool isfull=true;
node *creat(int pl,int pr,int il,int ir)
{
    if(pl>pr) return NULL;
    node *root=new node;
    root->data=post[pr];
    int k;
    for(k=il;k<=ir;k++) if(post[pr]==ino[k]) break;
    int numleft=k-il;
    root->l=creat(pl,pl+numleft-1,il,k-1);
    root->r=creat(pl+numleft,pr-1,k+1,ir);
    return root;
}
void DFS(node *root,int fa,int depth)
{
    if(root==NULL) return;
    int temp=root->data;
    stu[temp].parent=fa;
    stu[temp].level=depth;
    if(root->l!=NULL) stu[temp].lc=root->l->data;
    if(root->r!=NULL) stu[temp].rc=root->r->data;
    if(root->l==NULL&&root->r!=NULL) isfull=false;
    if(root->l!=NULL&&root->r==NULL) isfull=false;
    DFS(root->l,temp,depth+1);
    DFS(root->r,temp,depth+1);
}
int main()
{
    cin>>n;
    for(int i=0;i<n;i++) cin>>post[i];
    for(int i=0;i<n;i++) cin>>ino[i];
    node *root=creat(0,n-1,0,n-1);
    DFS(root,-1,1);
    cin>>k;
    getchar();
    string str,temp;
    while(k--)
    {
        getline(cin,str);
        bool flag=true;
        if(str=="It is a full tree") flag=isfull;
        else
        {
            int a=0,b=0;
            temp=str;
            while(isdigit(temp[0])) { a=a*10+temp[0]-'0'; temp.erase(temp.begin()); }
            if(temp.substr(0,4)==" and")
            {
                while(!isdigit(temp[0])) temp.erase(temp.begin());
                while(isdigit(temp[0])) { b=b*10+temp[0]-'0'; temp.erase(temp.begin()); }
                if(temp==" are siblings")
                {
                    if(stu[a].parent!=stu[b].parent) flag=false;
                }
                else if(stu[a].level!=stu[b].level) flag=false;
            }
            else
            {
                if(temp==" is the root") { if(a!=root->data) flag=false; }
                else if(temp.substr(0,18)==" is the parent of ")
                {
                    if(stu[stoi(temp.substr(18))].parent!=a) flag=false;
                }
                else if(temp.substr(0,22)==" is the left child of ")
                {
                    if(stu[stoi(temp.substr(22))].lc!=a) flag=false;
                }
                else if(temp.substr(0,23)==" is the right child of ")
                {
                    if(stu[stoi(temp.substr(23))].rc!=a) flag=false;
                }
            }
        }
        if(flag) printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}
  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值