PAT-2018年冬季考试-乙级

1091 N-自守数

代码:

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

int T;

int A(int a) {
    int cnt = 0;
    while(a) {
        a /= 10;
        cnt ++;
    }
    return cnt;
}

int Pow(int a, int b) {
    int ans = 1;
    for(int i = 1; i <= b; i ++)
        ans *= a;
    return ans;
}

int main() {
    scanf("%d", &T);
    while(T --) {
        int x;
        scanf("%d", &x);
        int c = A(x);
        int sum = x * x;
        int temp = 0, out = 0;
        for(int i = 1; i <= 9; i ++) {
            if((sum * i) % Pow(10, c) == x) {
                temp = i;
                out = sum * i;
                break;
            }
        }
        if(temp == 0)
            printf("No\n");
        else {
            printf("%d %d\n", temp, out);
        }
    }
    return 0;
}
View Code

1092 最好吃的月饼

代码:

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

int N, M;


struct Node{
    int kind;
    int price;
    int sum;
}node[1010];

bool cmp(const Node& a, const Node& b) {
    if(a.sum != b.sum)
        return a.sum > b.sum;
    else return a.kind < b.kind;
}

int main() {
    scanf("%d%d", &N, &M);
    for(int i = 0; i < M; i ++) {
        for(int j = 1; j <= N; j ++) {
            int x;
            scanf("%d", &x);
            node[j].kind = j;
            node[j].sum += x;
        }
    }
    int cnt = 0;
    sort(node + 1, node + N + 1, cmp);
    printf("%d\n", node[1].sum);
    for(int i = 1; i <= N; i ++) {
        if(node[i].sum == node[1].sum)
            cnt ++;
    }

    for(int i = 1; i <= cnt; i ++) {
        printf("%d", node[i].kind);
        printf("%s", i != cnt ? " " : "\n");
    }
    return 0;
}
View Code

1093 字符串 A + B

代码:

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

string A, B;

int main() {
    getline(cin, A);
    getline(cin, B);
    int la = A.length(), lb = B.length();
    map<char, int> mp;
    for(int i = 0; i < la; i ++) {
        if(mp[A[i]] == 0) {
            printf("%c", A[i]);
            mp[A[i]] ++;
        }
    }
    for(int i = 0; i < lb; i ++) {
        if(mp[B[i]] == 0) {
            printf("%c", B[i]);
            mp[B[i]] ++;
        }
    }
    printf("\n");
    return 0;
}
View Code

1094 谷歌的招聘

代码:

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

char s[1010];
int K, len;

bool isPrime(int x) {
    if(x == 1 || x == 0) return false;
    if(x == 2) return true;
    for(int i = 2; i * i <= x; i ++)
        if((x % i) == 0) return false;
    return true;
}

int main() {
    scanf("%d%d", &len, &K);
    scanf("%s", s);
    int ans;
    bool flag = false;
    vector<int> out;
    for(int i = 0; i <= len - K; i ++) {
        ans = 0;
        out.clear();
        for(int j = i; j < i + K; j ++) {
            ans = ans * 10 + s[j] - '0';
            out.push_back((s[j] - '0'));
        }
        if(isPrime(ans)) {
            flag = true;
            break;
        }
    }
    if(flag) {
        for(int i = 0; i < K; i ++)
            printf("%d", out[i]);
    }
    if(!flag)
        printf("404\n");
    return 0;
}
View Code

1095 解码 PAT 准考证

代码:

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

const int maxn = 1e5 + 10;

struct Node{
    char id[55];
    int score;
}node[maxn], s1[maxn], s2[maxn];

Node asd;

struct N3{
    int room;
    int ren;
}num[maxn], endd[maxn];

bool cmpp(const N3& a, const N3& b) {
    if(a.ren != b.ren)
        return a.ren > b.ren;
    else return a.room < b.room;
}

bool cmp1(const Node& a, const Node& b) {
    if(a.score != b.score)
        return a.score > b.score;
    else return strcmp(a.id, b.id) < 0;
}

int main() {
    int N, M;
    scanf("%d%d", &N, &M);
    int zlr = 0;
    for(int i = 1; i <= N; i ++) {
        scanf("%s%d", asd.id, &asd.score);
        if(asd.score >= 0 && asd.score <= 100) {
            zlr ++;
            node[zlr].score = asd.score;
            strcpy(node[zlr].id, asd.id);
        }
    }


    for(int t = 1; t <= M; t ++) {
        int x;
        char op[55];
        scanf("%d %s", &x, op);
        if(x == 1) {
            int cnt1 = 0;
            for(int i = 1; i <= zlr; i ++) {
                if(node[i].id[0] == op[0]) {
                    cnt1 ++;
                    strcpy(s1[cnt1].id, node[i].id);
                    s1[cnt1].score = node[i].score;
                }
            }
            printf("Case %d: %d %s\n", t, x, op);
            if(cnt1 == 0)
                printf("NA\n");
            else {
                sort(s1 + 1, s1 + 1 + cnt1, cmp1);
                for(int i = 1; i <= cnt1; i ++)
                    printf("%s %d\n", s1[i].id, s1[i].score);
            }
        } else if(x == 2) {
            int lx = strlen(op);
            int c;
            int renshu = 0, zf = 0;
            for(int i = 1; i <= zlr; i ++) {
                c = 0;
                for(int j = 0; j < lx; j ++)
                    if(node[i].id[j + 1] == op[j])
                        c ++;
                if(c == lx) {
                    renshu ++;
                    zf += node[i].score;
                }
            }
            printf("Case %d: %d %s\n", t, x, op);
            if(renshu == 0)
                printf("NA\n");
            else {
                printf("%d %d\n", renshu, zf);
            }
        } else if(x == 3){
            for(int kk = 0; kk < maxn; kk ++) {
                endd[kk].ren = 0;
                endd[kk].room = 0;
                num[kk].ren = 0;
                num[kk].room = 0;
            }
            int lc = strlen(op);
            int d = 0;
            int nnn = 0;
            int naa = 0;
            for(int i = 1; i <= zlr; i ++) {
                d = 0;
                for(int j = 0; j < lc; j ++) {
                    if(node[i].id[j + 4] == op[j])
                        d ++;
                }
                if(d == 6) {
                    naa ++;
                    int rec = 0;
                    for(int h = 1; h <= 3; h ++)
                        rec = rec * 10 + (node[i].id[h] - '0');
                    num[rec].room = rec;
                    num[rec].ren ++;
                }
            }
            printf("Case %d: %d %s\n", t, x, op);
            if(naa == 0)
                printf("NA\n");
            else {
                for(int r = 0; r <= 999; r ++) {
                    if(num[r].ren != 0) {
                        nnn ++;
                        endd[nnn].ren = num[r].ren;
                        endd[nnn].room = num[r].room;
                    }
                }
                sort(endd + 1, endd + 1 + nnn, cmpp);
                for(int u = 1; u <= nnn; u ++)
                    printf("%d %d\n", endd[u].room, endd[u].ren);
            }
        } else printf("Case %d: %d %s\nNA\n", t, x, op);
    }
    return 0;
}
View Code

 

最后一道题真的是看了很久才发现错误 第一次考试太紧张 希望下次甲级也会顺利吧  可惜的是证书还没拿到拿到之后再补图 8 之后的日子可以继续安心学新的东西了 想想就开心呢

posted @ 2018-12-10 13:20  丧心病狂工科女  阅读(1573)  评论(3编辑  收藏  举报