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;
}

2019春季PAT甲级题解相关推荐

  1. 【PAT】2021年春季PAT甲级题解

    文章目录 1. Arithmetic Progression of Primes (20 分) 题意 解法 暴力+剪枝 2. Lab Access Scheduling (25 分) 题意 解法 排序 ...

  2. 2019年9月8日秋季PAT甲级题解A1163(7-4)Dijkstra Sequence

    2019年9月8日秋季PAT甲级题解 A1163(7-4)Dijkstra Sequence (30 分) #include<iostream> #include<vector> ...

  3. 2019秋季PAT甲级考试总结:努力+策略+运气

    鉴于这两天有很多网友联系我问这次考试的题解,所以我干脆就花点时间把C++题解整理出来了,见文末 经过一两个月的备战PAT,在今天终于画上了一个圆满的句号,取得了满分的成绩. 我是在南京的金陵科技学院考 ...

  4. 【PAT甲级题解记录】1148 Werewolf - Simple Version (20 分)

    [PAT甲级题解记录]1148 Werewolf - Simple Version (20 分) 前言 Problem:1148 Werewolf - Simple Version (20 分) Ta ...

  5. 2019春季PAT考试甲级答案

    20190302春季PAT考试甲级答案 7-1 Sexy Primes (20 分) Sexy primes are pairs of primes of the form (p, p+6), so- ...

  6. 2019秋季PAT甲级_C++题解

    2019 秋季 PAT (Advanced Level) C++题解 考试拿到了满分但受考场状态和知识水平所限可能方法不够简洁,此处保留记录,仍需多加学习.备考总结(笔记目录)在这里 7-1 Fore ...

  7. 2021年春季PAT甲级考试

    作为一个绩点不高.牌子不硬的退役ACMer,去年冬天在nvpy的鼓励下,决定先试试PAT为考研做准备,于是认认真真把所有20分的题目过了一遍.放寒假以后,就开始练习所有25分的题目,并且每道题做完都总 ...

  8. 2022夏PAT甲级题解 by小柳2022.6.7

    公众号题解在此 题目在此 因为某些原因没有报名这次的PAT甲级考试, 所以在上班时间摸鱼写了一下

  9. PAT-2022年春季考试 - 甲级题解

    试卷在此 7-1 Simple Lie Detection (20 分) 作者 陈越 单位 浙江大学 代码长度限制 16 KB 时间限制 400 ms 内存限制 64 MB Lie detection ...

  10. 2019秋季PAT甲级考试心得

    第一道题一共四个测试点,做了一个小时,有两个测试点没过,得了12/20分 做题的时候遇到一个问题,就是include<math.h>之后,使用pow(10,n)计算报错具有多个定义之类的( ...

最新文章

  1. 并发编程之Java内存模型
  2. ubuntu16.04将普通用户提升至root权限
  3. python 生意_本周互联网关注(2015515):劳动人民的生意经、python好还是go好
  4. java情书_Java情书已写好,就差妹子了!
  5. 智慧职教云答案在哪里找_职教云网课答案在线查询,职教云答案查询,智慧职教云答案在哪里找到...
  6. mysql 优化max_Mysql 优化
  7. 只需2行代码,1分钟教你实现微信多开
  8. 第一节 细胞是生命活动的基本单位
  9. 【软件工程】机房文档--可行性研究报告
  10. 用Typora+PicGo搞定多个平台发文和微信公众号排版
  11. 团队内的沟通方式:网络 OR 当面
  12. codeforces 711 C. Coloring Trees (dp)
  13. DosBox下的debug信息解释
  14. 这些Linux技能你会了,面试官:哎呦小伙子不错哦!
  15. 【洛谷P3960】列队题解
  16. 前端自学HTML笔记之二【超链接】
  17. 工作一年了,回顾过去一年的点滴(二)
  18. BMW - 下一代EE架构如何实现
  19. Python WordCloud 文本分析 生成词云图
  20. 基于SpringBoot的健身房管理系统(提供源码)

热门文章

  1. “海潮效应”下,聚则“生”分则“亡”360奏响“经济复苏集结号”
  2. cpuz测试分数天梯图_2018年9月CPU天梯图 桌面级处理器天梯图最新版
  3. mac mini 蓝牙_Mac Mini
  4. 进程间的相互通讯 C++
  5. 使用 Tenderly 为区块链交易设置警报通知
  6. 3.样条曲线之B样条曲线
  7. win10 屏幕保护时间到了不触发_你真的了解Win10么?网友教你玩转Win10!
  8. Reactjs源码分析
  9. android 判断连接的网络是移动?联通?电信?
  10. 程序员都是段子手,注释都带魔性