PAT甲级2019冬季考试题解

Posted zhanglichen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT甲级2019冬季考试题解相关的知识,希望对你有一定的参考价值。

A Good In C
纯模拟题,用string数组读入数据,注意单词数量的判断

#include<bits/stdc++.h>
using namespace std;
const int maxn=1010;
string a[27][8];
int main () {
    for (int i=1;i<=26;i++) {
        for (int j=1;j<=7;j++)
        cin>>a[i][j];
    }
    string t;
    getchar ();
    getline (cin,t);
    string s[1050];
    int top=0;
    for (int i=0;i<t.length();i++) {
        if (t[i]>=A&&t[i]<=Z) s[top]+=t[i];
        else if (t[i-1]>=A&&t[i-1]<=Z) top++;
    }
    if (top!=0&&!(t[t.length()-1]>=A&&t[t.length()-1]<=Z)) top--;
    for (int k=0;k<=top;k++) {
        for (int j=1;j<=7;j++) {
            for (int i=0;i<s[k].length();i++) {
                if (i!=0) printf (" ");
                cout<<a[s[k][i]-A+1][j];
            }
            cout<<endl;
        }
        //for (int j=1;j<=7;j++)
        if (k!=top) printf ("
");
    }
    //printf ("
");
    return 0;
}

B Block Reversing
链表常规题,用结构体静态建立链表,有两个注意点:1、给出的结点里会有无关结点。2、链表里的元素个数能不能整除k要分开判断。

#include<bits/stdc++.h>
using namespace std;
const int maxn=100001;
struct node {
    int address;
    int data;
    int next;
}Node[maxn];
int main () {
    int head,N,K;
    scanf ("%d %d %d",&head,&N,&K);
    int address,data,next;
    for (int i=0;i<N;i++) {
        scanf ("%05d %d %05d",&address,&data,&next);
        Node[address].address=address;
        Node[address].data=data;
        Node[address].next=next;
    }
    vector<node> vi;
    while (head!=-1) {
        vi.push_back(Node[head]);
        head=Node[head].next;
    }
    vector<int> vp[100];
    if (vi.size()%K!=0) {
        printf ("%05d %d ",vi[vi.size()/K*K].address,vi[vi.size()/K*K].data);
        for (int i=vi.size()/K*K+1;i<vi.size();i++)
        printf ("%05d
%05d %d ",vi[i].address,vi[i].address,vi[i].data);
        for (int i=vi.size()/K;i>0;i--) {
            for (int j=(i-1)*K;j<i*K;j++)
            printf ("%05d
%05d %d ",vi[j].address,vi[j].address,vi[j].data);
        }
        printf ("-1");
    }
    else {
        printf ("%05d %d ",vi[vi.size()-K].address,vi[vi.size()-K].data);
        for (int i=vi.size()/K;i>0;i--) {
            int flag=0;
            if (i==vi.size()/K) flag=1;
            for (int j=(i-1)*K+flag;j<i*K;j++)
            printf ("%05d
%05d %d ",vi[j].address,vi[j].address,vi[j].data);
        }
        printf ("-1");
    }
    return 0;
} 

C Summit
个人认为全场最简单的一题,也是第一道ac的题目,就是图论,根据题意暴力做即可~

#include<bits/stdc++.h>
using namespace std;
const int maxn=1010;
const int inf=1e9;
int g[maxn][maxn];
int N,M,K,L,a[maxn];
void init () {
    for (int i=1;i<=N;i++)
    for (int j=1;j<=N;j++)
    g[i][j]=inf; 
}
int main () {
    scanf ("%d %d",&N,&M);
    int u,v;
    init ();
    for (int i=0;i<M;i++) {
        scanf ("%d %d",&u,&v);
        g[u][v]=g[v][u]=1;
    }
    scanf ("%d",&K);
    for (int i=0;i<K;i++) {
        int flag1=1,flag2=0,hash1[maxn]={0};
        scanf ("%d",&L);
        for (int j=0;j<L;j++) {
            scanf ("%d",&a[j]);
            hash1[a[j]]=1;
        }
        for (int j=0;j<L;j++) {
            for (int k=j+1;k<L;k++) {
                if (g[a[j]][a[k]]==inf) {
                    flag1=0;break;
                }
            }
            if (flag1==0) break;
        }
        if (flag1==0) {
            printf ("Area %d needs help.
",i+1);
            continue;
        }
        int u;
        for (int j=1;j<=N;j++) {
            if (hash1[j]==0) {
                int flag3=0;
                for (int k=0;k<L;k++) 
                if (g[a[k]][j]==inf) flag3++;
                if (flag3==0) {
                    u=j;
                    flag2=1;
                    break;
                }
            }
            if (flag2==1) break;
        }
        if (flag1==1&&flag2==1) {
            printf ("Area %d may invite more people, such as %d.
",i+1,u);
        }
        else if (flag1==1&&flag2==0) {
            printf ("Area %d is OK.
",i+1);
        }
    }
    return 0;
}

D Cartesian Tree
一开始以为是堆的算法,写了很久,其实并不是。一颗树,结点数据符合小顶堆的规则,给出它的中序遍历,要求输出层序。解决方法是先找出给定序列里最小的点作为根结点,再以根节点为标志,找根节点左边和右边各自最小的结点,作为根节点的左右子树,递归处理。

#include<bits/stdc++.h>
using namespace std;
const int maxn=1010;
struct node {
    int data;
    node * left;
    node * right;
};
int a[maxn];
node * create (int inL,int inR,int inRoot) {
    if (inRoot==1e9) return NULL;
    node * root=new node;
    root->data=a[inRoot];
    int inLeft=1e9,inRight=1e9;
    int min=1e9;
    for (int i=inL;i<inRoot;i++) {
        if (a[i]<min) {
            min=a[i];
            inLeft=i;
        }
    }
    root->left=create (inL,inRoot-1,inLeft);
    min=1e9;
    for (int i=inRoot+1;i<=inR;i++) {
        if (a[i]<min) {
            min=a[i];
            inRight=i;
        }
    }
    root->right=create (inRoot+1,inR,inRight);
    return root;
}
void bfs (node * root) {
    queue<node *> q;
    q.push(root);
    while (!q.empty()) {
        node * now=q.front();
        q.pop();
        if (now!=root) printf (" ");
        printf ("%d",now->data);
        if (now->left) q.push(now->left);
        if (now->right) q.push(now->right);
    } 
}
int main () {
    int N;
    scanf ("%d",&N);
    for (int i=1;i<=N;i++) {
        scanf ("%d",&a[i]);
    }
    int min=1e9,inRoot=-1;
    for (int i=1;i<=N;i++) {
        if (a[i]<min) {
            min=a[i];
            inRoot=i;
        }
    }
    //printf ("%d
",inRoot);
    node * root=create (1,N,inRoot);
    bfs (root);
    return 0;
}

 

以上是关于PAT甲级2019冬季考试题解的主要内容,如果未能解决你的问题,请参考以下文章

PAT(甲级)2019年冬季考试 7-2 Block Reversing

PAT(甲级)2019年春季考试题解

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

PAT(甲级)2018年冬季考试

PAT(甲级)2018年冬季考试 7-2 Decode Registration Card of PAT

PAT2021年冬季考试甲级,摸鱼游记92分