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;
}
PAT(甲级)2019年春季考试的更多相关文章
- PAT甲级2019冬季考试题解
A Good In C纯模拟题,用string数组读入数据,注意单词数量的判断 #include<bits/stdc++.h> using namespace std; ; ][]; in ...
- PAT甲级满分攻略|记一次考试经历
一次考试经历 今天是"大雪",很冷. 来到隔壁的学校考试,记得上一次来河中医是两年前大一刚开学吧,那天晚上印象比较深刻,6个室友骑车到处闲逛.当时还不会Hello world. 很 ...
- PAT(甲级)2017年春季考试
PAT(甲级)2017年春季考试 A.Raffle for Weibo Followers #include<bits/stdc++.h> using namespace std; int ...
- 2019秋季PAT甲级_备考总结
2019 秋季 PAT 甲级 备考总结 在 2019/9/8 的 PAT 甲级考试中拿到了满分,考试题目的C++题解记录在这里,此处对备考过程和考试情况做一个总结.如果我的方法能帮助到碰巧点进来的有缘 ...
- PAT甲级考前整理(2019年3月备考)之三,持续更新中.....
PAT甲级考前整理一:https://www.cnblogs.com/jlyg/p/7525244.html,主要讲了131题的易错题及坑点 PAT甲级考前整理二:https://www.cnblog ...
- PAT甲级考前整理(2019年3月备考)之一
转载请注明出处:https://www.cnblogs.com/jlyg/p/7525244.html 终于在考前,刷完PAT甲级131道题目,不容易!!!每天沉迷在刷题之中而不能超脱,也是一种 ...
- 2019秋季PAT甲级_C++题解
2019 秋季 PAT (Advanced Level) C++题解 考试拿到了满分但受考场状态和知识水平所限可能方法不够简洁,此处保留记录,仍需多加学习.备考总结(笔记目录)在这里 7-1 Fore ...
- 2021.9.12周六PAT甲级考试复盘与总结
周六PAT甲级考试复盘与总结 先说结论:仍未步入"高手"行列:现在的学习节奏与方法是对的,有十万分的必要坚持下去. 题目 知识点 分数 T1 前缀和.二分 11 / 20 T2 排 ...
- pat甲级考试+pat1051+1056
同上一篇博客: 贪心题目我已经刷了将近30道了,由于那几天考驾照就没写,以后有空的时候补过来吧,都在codeblock里 pat的题也刷了点,acwing 的题也刷了点,基本都攒下了.以后也会慢慢补过 ...
随机推荐
- Django学习day8——admin后台管理和语言适应
Django最大的优点之一,就是体贴的为你提供了一个基于项目model创建的一个后台管理站点admin.这个界面只给站点管理员使用,并不对大众开放. 1. 创建管理员用户 (django) E:\Dj ...
- SSHD服务安全的连接
SSHD服务 SSH 安全的远程连接 OpenSSH 工具 centos服务端的包:openssh-server centos客户端的包:openssh-clients 主要配置文件一般安装完成后再/ ...
- GitHub + jsDelivr + PicGo + Imagine 打造稳定快速、高效免费图床
GitHub + jsDelivr + PicGo + Imagine 打造稳定快速.高效免费图床 前言 为什么要使用图床呢? 因为在不同平台发布同一篇文章的时候,最一个痛苦的点就是,图片存储问题,各 ...
- linux 打包 | autoconf 使用方法
面试题 嵌入式 0x10道题目 宏定义 #define 宏体 宏体 (大写) #define SECOND_OF_YEAR (365*24*3600)UL 可移植性 数据声明 一个存有10个指针的数组 ...
- Convolutional Sequence to Sequence Learning 论文笔记
目录 简介 模型结构 Position Embeddings GLU or GRU Convolutional Block Structure Multi-step Attention Normali ...
- 高德地图3D菱形 区域点击搜索
更新一波吧 <!doctype html> <html lang="zh-CN"> <head> <!-- 原始地址://webapi.a ...
- Project Euler 60: Prime pair sets
素数3, 7, 109, 673很有意思,从中任取两个素数以任意顺序拼接起来形成的仍然是素数.例如,取出7和109,7109和1097都是素数.这四个素数的和是792,是具有这样性质的四个素数的最小的 ...
- tp5验证码的使用
<div><img id="verify_img" src="{:captcha_src()}" alt="验证码" on ...
- Thinkphp5与QueryList,也可以实现采集(爬虫)页面功能
QueryList 是什么 QueryList是一套用于内容采集的PHP工具,它使用更加现代化的开发思想,语法简洁.优雅,可扩展性强.相比传统的使用晦涩的正则表达式来做采集,QueryList使用了更 ...
- [LC]219题 Contains Duplicate II (存在重复元素 II )
①英文题目: Given an array of integers and an integer k, find out whether there are two distinct indices ...