PAT(甲级)2018年冬季考试

1152 Google Recruitment

思路:判断素数

#include<bits/stdc++.h>
using namespace std;

const int maxn = 1100;
int a[maxn];
int n,k;

long long getNum(int pos){
    long long x = 0;
    for(int i=pos;i<=pos+k-1;i++){
        x = x*10 + a[i];
    }
    return x;
}

bool prime(long long x){
    if(x < 2) return false;
    if(x == 2) return true;
    int endss = sqrt(x);
    for(int i=2;i<=endss;i++){
        if(x%i == 0) return false;
    }
    return true;
}

void print(long long  x){
    int len = 0;
    long long temp = x;
    while(temp){
        temp/=10;
        len++;
    }
    if(len == k) {
        printf("%lld",x);
        return;
    }
    int t = k - len;
    while(t--)printf("0");
    printf("%lld",x);
}

int main(){
    scanf("%d %d",&n,&k);
    for(int i=1;i<=n;i++)
        scanf("%1d",&a[i]);
    bool flag = false;
    for(int i=1;i+k-1<=n;i++){
        long long x = getNum(i);
        if(prime(x)){
            flag = true;
            print(x);
            break;
        }
    }
    if(flag == false) puts("404");
    return 0;
} 

1153 Decode Registration Card of PAT

思路:结构体排序,map计数,string.c_str()把字符串转成字符数组后可以使用print输出
注意:使用太多cin、cout太慢了会超时

#include<bits/stdc++.h>
//#include<unordered_map>
using namespace std;

/*
Cin和Cout不能用太多啦
除了string 和 char类型的输入 最好把Cin替换成scanf
Cout全部换成printf 对于string类型的printf 需要将string转换成字符数组
string.c_str();
*/

const int maxn = 1e4+10;
int n,m;
struct node{
    char type;
    int site;
    string date;
    int num;
    int score;
    string all;
};
struct node stu[maxn];
vector<node> ans;

bool cmp1(node s1,node s2){
    if(s1.score == s2.score) {
        s1.all < s2.all;
    }
    return s1.score > s2.score;
}

bool cmp2(node s1,node s2){
    if(s1.score > s2.score) return true;
    else if(s1.score < s2.score) return false;
    return s1.all < s2.all;
}
int mm[1001];
struct nod{
    int site;
    int num;
    nod(int sss,int nnn){
        site = sss;
        num = nnn;
    }
};
vector<nod> ans3;
bool cmp3(nod s1,nod s2){
    if(s1.num == s2.num) return s1.site < s2.site;
    return s1.num > s2.num;
}
int main(){
//  cin>>n>>m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        string str;
        int score;
//      cin>>str>>score;
        cin>>str;
//      scanf("%s",&str);
        scanf("%d",&score);
        stu[i].all = str;
        stu[i].score = score;
        stu[i].type = str[0];
        stu[i].site = (str[1] - '0') * 100 + (str[2] - '0') * 10 + (str[3] - '0');
        stu[i].date = str.substr(4,6);
        stu[i].num = (str[10] - '0') * 100 + (str[11] - '0') * 10 + (str[12] - '0');
    }
    for(int Case=1;Case<=m;Case++){
        int Type;
//      cin>>Type;
        scanf("%d",&Type);
        int cnt = 0;
        ans.clear();
        ans3.clear();
        if(Type == 1){
            char type;
            cin>>type;
            printf("Case %d: 1 %c\n",Case,type);
            for(int i=1;i<=n;i++){
                if(stu[i].type == type){
                    ans.push_back(stu[i]);
                }
            }
            cnt = ans.size();
            if(cnt == 0){
                puts("NA");
            }else{
                sort(ans.begin(),ans.end(),cmp2);
                for(int i=0;i<=cnt-1;i++){
//                  cout<<ans[i].all<<" "<<ans[i].score<<endl;
                    printf("%s ",ans[i].all.c_str());
                    printf("%d\n",ans[i].score);
                }
            }
        }else if(Type == 2){
            int site;
//          cin>>site;
            scanf("%d",&site);
            printf("Case %d: 2 %d\n",Case,site);
            int tot = 0;
            for(int i=1;i<=n;i++){
                if(stu[i].site == site){
                    tot += stu[i].score;
                    cnt++;
                }
            }
            if(cnt == 0){
                puts("NA");
            }else{
                printf("%d %d\n",cnt,tot);
            }
        }else if(Type == 3){
            string date;
            cin>>date;
            printf("Case %d: 3 ",Case);
//          cout<<date<<endl;
            printf("%s\n",date.c_str());
            for(int i=100;i<=999;i++) mm[i] = 0;
            for(int i=1;i<=n;i++){
                if(stu[i].date == date){
                    mm[stu[i].site]++;
                }
            }
            for(int i=100;i<=999;i++){
                if(mm[i] != 0){
                    ans3.push_back(nod(i,mm[i]));
                }
            }
//          map<int, int> mp;
//          for(int i=1;i<=n;i++){
//              if(stu[i].date == date) mp[stu[i].site]++;
//          }
//          map<int, int>::iterator it = mp.begin();
//          while(it!=mp.end()){
//              ans3.push_back(nod(it->first,it->second));
//              it++;
//              cnt++;
//          }
            cnt = ans3.size();
            if(cnt == 0){
                puts("NA");
            }else{
                sort(ans3.begin(),ans3.end(),cmp3);
                for(int i=0;i<=cnt-1;i++){
//                  cout<<ans3[i].site<<" "<<ans3[i].num<<endl;
                    printf("%d %d\n",ans3[i].site,ans3[i].num);
                }
            }
        }
    }
    return 0;
} 

1154 Vertex Coloring

思路:图论,dfs遍历检查相邻结点的颜色

#include<bits/stdc++.h>
using namespace std;

const int maxn = 10010;
int n,m,k;
vector<int> g[maxn];
int col[maxn];
int vis[maxn];
set<int> se;
bool flag = false;

void dfs(int x){
    if(flag == false) return;
    vis[x] = 1;
    for(int i=0;i<g[x].size();i++){
        int v = g[x][i];
        if(col[x] == col[v]) flag = false;
        if(!vis[v]) dfs(v);
    }
}

int main(){
    cin>>n>>m;
    for(int i=1;i<=m;i++){
        int u,v;
        cin>>u>>v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    cin>>k;
    while(k--){
        se.clear();
        for(int i=0;i<n;i++){
            cin>>col[i];
            se.insert(col[i]);
            vis[i] = 0;
        }
        flag = true;
        for(int i=0;i<n;i++){
            if(flag == false) break;
            if(vis[i]) continue;
            dfs(i);
        }
        if(flag) cout<<se.size()<<"-coloring"<<endl;
        else cout<<"No"<<endl;
    }
    return 0;
} 

1155 Heap Paths

思路:给定序列,判断是否大根堆、小根堆(特殊二叉树)

#include<bits/stdc++.h>
using namespace std;

const int maxn = 50010;
int n;
int g[maxn];
vector<int> v;
bool flagMin = false;
bool flagMax = false;
bool flag = true;

void dfs(int x){
    v.push_back(g[x]);
    if(x * 2 + 1 <= n){
        dfs(x * 2 + 1);
    }
    if(x * 2 <= n){
        dfs(x * 2);
    }
    if(x * 2 + 1 > n && x * 2 > n){
        int temp = v[0];
        if(flag){
            int cnt = 0;
            for(int i=0;i<v.size();i++){
                if(i > 0 && v[i] >= v[i-1]) cnt++;
            }
            if(cnt == v.size() - 1) flagMax = true;
            else if(cnt == 0) flagMin = true;
            else flag = false;
        }
        for(int i=0;i<v.size();i++){
            if(i == 0) cout<<v[i];
            else cout<<" "<<v[i];
        }
        if(v.size() != 0) cout<<endl;
    }
    v.erase(v.end()-1);
}

int main(){
    cin>>n;
    for(int i=1;i<=n;i++) cin>>g[i];
    dfs(1);
    if(flag == false){
        cout<<"Not Heap"<<endl;
    }else if(flagMin == true && flagMax == false){
        cout<<"Max Heap"<<endl;
    }else if(flagMin == false && flagMax == true){
        cout<<"Min Heap"<<endl;
    }else{
        cout<<"Not Heap"<<endl;
    }
    return 0;
}
 

原文地址:https://www.cnblogs.com/fisherss/p/11631271.html

时间: 2024-07-30 15:29:34

PAT(甲级)2018年冬季考试的相关文章

PAT甲级考试题库1001 A+B Format 代码实现及相关知识学习

准备参加九年九月份的PAT甲级证书考试,对网站上的题目进行总结分析: 1001题 A+B Format (20 分) Calculate a+b and output the sum in standard format -- that is, the digits must be separated into groups of three by commas (unless there are less than four digits). 计算a+b的值并以一定格式输出其和sum(数字需要

PAT甲级1005 Spell It Right

题目:PAT甲级 1005 题解:水题.看到题目的第一时间就在想一位一位的mod,最后一加一转换就完事了.结果看到了N最大为10的100的次方,吓得我赶紧放弃这个想法... 发现碰到这种情况用字符串十分好用,这道题应该考察的就是这一点.大致思路就是把数字的每一位放到字符串中,然后通过ASCII码得到每一位的相加结果num,然后把num一位一位的放到stack中,使用stack是因为它先进先出的特性,最后输出就行了. 代码: 1 #include<cstdio> 2 #include<qu

PAT甲级考前整理

终于在考前,刷完PAT甲级130道题目,不容易!!!每天沉迷在刷题之中而不能超脱,也是一种境界.PAT甲级题目总的说卡题目的比较多,卡测试点的比较少,有些题目还会有题意混淆,这点就不吐槽了吧.静下心来耍这130道题,其实磨练的是一种态度与手感,养成的是一种习惯.热爱AC没有错!! 130道题目主要的考点: 1.排序:快速排序,直接插入排序,希尔排序,分治排序,堆排序. 2.图论:拓扑排序.最短路径.深度搜索.广度搜索. 3.树:树的遍历.完全二叉树.AVL. 4.其他:并查集,模拟,哈希.背包.

PAT甲级专题|最短路

PAT甲级最短路 主要算法:dijkstra 求最短最长路.dfs图论搜索. 1018,dijkstra记录路径 + dfs搜索路径最值 25分,错误点暂时找不出.. 如果只用dijkstra没法做,只能得20分 #include<bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const int maxn = 510; int cmax,n,ter,m; int caps[maxn]; int g[maxn][m

【PAT甲级】1070 Mooncake (25 分)(贪心水中水)

题意: 输入两个正整数N和M(存疑M是否为整数,N<=1000,M<=500)表示月饼的种数和市场对于月饼的最大需求,接着输入N个正整数表示某种月饼的库存,再输入N个正数表示某种月饼库存全部出手的利润.输出最大利润. trick: 测试点2可能包含M不为整数的数据.(尽管题面说明M是正整数,可是根据从前PAT甲级题目的经验,有可能不是整数.....) 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using names

2019年春PAT甲级考试

这次考试不是很理想,一道题目没能做完. 自己原因差不多三条: 1.自己实力不够,准备时间也有点仓促,自己没能做到每道题目都有清晰的思路. 2.考试的心里素质不行,因为设备原因东奔西跑浪费了听过时间,自己心态有点不稳. 3.考场选择的太糟糕了,老师超级不负责任,设施也真的无语,电脑也无法复制测试用例,测试的时候必须要手动输入:老旧的机器,编译器只有2010年的vc和vs. 接下来自己要保持每天的练习,9月份换个考场再参加考试.今年的题目第四道反而不是很难,但是第三道题目条件比较多,因为中途更换机器

2019冬季PAT甲级第一题

1 #define HAVE_STRUCT_TIMESPEC 2 #include<bits/stdc++.h> 3 using namespace std; 4 string s[30][10]; 5 int ans[1007][1007]; 6 int num[1007]; 7 string t; 8 int main(){ 9 ios::sync_with_stdio(false); 10 cin.tie(NULL); 11 cout.tie(NULL); 12 for(int i=1;

2019冬季PAT甲级第二题

1 #define HAVE_STRUCT_TIMESPEC 2 #include<bits/stdc++.h> 3 using namespace std; 4 typedef struct{ 5 int add,data,nex; 6 }Node; 7 Node node[100007],ans[100007],ans2[100007]; 8 map<int,int>mp; 9 int main(){ 10 //ios::sync_with_stdio(false); 11 /

PAT-2019年冬季考试-乙级(题解)

很荣幸这次能够参加乙级考试,和大佬们同台竞技了一次,这篇博客,进行介绍这次2019冬季的乙级考试题解. 7-1 2019数列 (15分) 把 2019 各个数位上的数字 2.0.1.9 作为一个数列的前 4 项,用它们去构造一个无穷数列,其中第 n(>)项是它前 4 项之和的个位数字.例如第 5 项为 2, 因为 2,个位数是 2. 本题就请你编写程序,列出这个序列的前 n 项. 输入格式: 输入给出正整数 n(≤). 输出格式: 在一行中输出数列的前 n 项,数字间不要有空格. 输入样例: 1