PAT 真題題解

1012. 數字分類 (20)-PAT乙級真題

給定一系列正整數,請按要求對數字進行分類,並輸出以下5個數字:
A1 = 能被5整除的數字中所有偶數的和;
A2 = 將被5除後餘1的數字按給出順序進行交錯求和,即計算n1-n2+n3-n4…;
A3 = 被5除後餘2的數字的個數;
A4 = 被5除後餘3的數字的平均數,精確到小數點後1位;
A5 = 被5除後餘4的數字中最大數字。
輸入格式:
每個輸入包含1個測試用例。每個測試用例先給出一個不超過1000的正整數N,
隨後給出N個不超過1000的待分類的正整數。數字間以空格分隔。
輸出格式:
對給定的N個正整數,按題目要求計算A1~A5並在一行中順序輸出。
數字間以空格分隔,但行末不得有多餘空格。
若其中某一類數字不存在,則在相應位置輸出“N”。
輸入樣例1:
13 1 2 3 4 5 6 7 8 9 10 20 16 18
輸出樣例1:
30 11 2 9.7 9
輸入樣例2:
8 1 2 4 5 6 7 9 16
輸出樣例2:
N 11 2 N 9
#include <iostream>
#include <cstdio>

using namespace std;

int main() {
    int a1 = 0, a2 = 0, a3 = 0, a5 = 0;
    float a4 = 0.0;
    int N;
    cin >> N;
    int *a;
    a = new int[N];

    for (int i = 0; i < N; ++i) {
        cin >> a[i];
    }

    int cnt1 = 0, cnt2 = 0, cnt3 = 0, cnt4 = 0, cnt5 = 0;
    int temp = 1;
    for (int i = 0; i < N; ++i) {
        if (a[i] % 5 == 0 && a[i] % 2 == 0) {
            a1 += a[i];
            cnt1++;
        }
        else if (a[i] % 5 == 1) {
            a2 = a2 + temp * a[i];
            temp = -temp;
            cnt2++;
        }
        else if (a[i] % 5 == 2) {
            a3++;
            cnt3++;
        }
        else if (a[i] % 5 == 3) {
            a4 += a[i];
            cnt4++;
        }
        else if (a[i] % 5 == 4) {
            if (a5 < a[i]) {
                a5 = a[i];
            }
            cnt5++;
        }
    }

    if (cnt1 == 0) {
        cout << "N ";
    } else {
        cout << a1 << " ";
    }
    if (cnt2 == 0) {
        cout << "N ";
    } else {
        cout << a2 << " ";
    }
    if (cnt3 == 0) {
        cout << "N ";
    } else {
        cout << a3 << " ";
    }
    if (cnt4 == 0) {
        cout << "N ";
    } else {
        printf("%.1f ", a4 / cnt4);
    }
    if (cnt5 == 0) {
        cout << "N";
    } else {
        cout << a5;
    }

    delete[] a;

    return 0;
}

1013. 數素數 (20)-PAT乙級真題

令Pi表示第i個素數。現任給兩個正整數M <= N <= 10^4,請輸出PM到PN的所有素數。
輸入格式:
輸入在一行中給出M和N,其間以空格分隔。
輸出格式:
輸出從PM到PN的所有素數,每10個數字佔1行,其間以空格分隔,但行末不得有多餘空格。
輸入樣例:
5 27
輸出樣例:
11 13 17 19 23 29 31 37 41 43
47 53 59 61 67 71 73 79 83 89
97 101 103
#include <iostream>
#include <cmath>

using namespace std;

bool isprime(int a) {
    for (int i = 2; i <= sqrt(a) ; ++i) {
        if (a % i == 0)
            return false;
    }
    return true;
}

int main() {
    int a, b;

    cin >> a >> b;
    int vec[10001];
    int k = 2;
    int i = 0;
    int t = 0;
    while (t < b) {
        if (isprime(k)) {
            t++;
            if (t >= a)
                vec[++i] = k;
        }
        k++;
    }

    int temp = 0;
    for (int j = 1; j <= i; j++) {
        if (j % 10 == 1) {
            ++temp;
            cout << vec[j];
            continue;
        }
        cout << " " << vec[j];
        if (++temp % 10 == 0)
            cout << endl;
    }

    return 0;
}

1014. 福爾摩斯的約會 (20)-PAT乙級真題

大偵探福爾摩斯接到一張奇怪的字條:“我們約會吧!3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d&Hyscvnm”。大偵探很快就明白了,字條上奇怪的亂碼實際上就是約會的時間“星期四 14:04”,因爲前面兩字符串中第1對相同的大寫英文字母(大小寫有區分)是第4個字母’D’,代表星期四;第2對相同的字符是’E’,那是第5個英文字母,代表一天裏的第14個鐘頭(於是一天的0點到23點由數字0到9、以及大寫字母A到N表示);後面兩字符串第1對相同的英文字母’s’出現在第4個位置(從0開始計數)上,代表第4分鐘。現給定兩對字符串,請幫助福爾摩斯解碼得到約會的時間。
輸入格式:
輸入在4行中分別給出4個非空、不包含空格、且長度不超過60的字符串。
輸出格式:
在一行中輸出約會的時間,格式爲“DAY HH:MM”,其中“DAY”是某星期的3字符縮寫,即MON表示星期一,TUE表示星期二,WED表示星期三,THU表示星期四,FRI表示星期五,SAT表示星期六,SUN表示星期日。題目輸入保證每個測試存在唯一解。
輸入樣例:
3485djDkxh4hhGE
2984akDfkkkkggEdsb
s&hgsfdk
d&Hyscvnm
輸出樣例:
THU 14:04
#include <iostream>

using namespace std;

int main() {
    string a, b, c, d;
    cin >> a >> b >> c >> d;
    unsigned long alen = a.length();
    unsigned long blen = b.length();
    unsigned long clen = c.length();
    unsigned long dlen = d.length();

    char tc[2];
    int i;
    i = 0;

    for (; i < min(alen, blen); i++) {
        if (a[i] == b[i] && (a[i] >= 'A' && a[i] <= 'G')) {
            tc[0] = a[i];
                break;
        }
    }
    for (++i; i < min(alen, blen); ++i) {
        if (a[i] == b[i] && ((a[i] >= '0' && a[i] <= '9') || (a[i] >= 'A' && a[i] <= 'N'))) {
            tc[1] = a[i];
            break;
        }
    }

    int temp = 0;
    for (int j = 0; j < min(clen, dlen); ++j) {
        if (c[j] == d[j] && ((c[j] >= 'a' && c[j] <= 'z') || (c[j] >= 'A' && c[j] <= 'Z'))) {
            temp = j;
            break;
        }
    }

    string string1;
    switch (tc[0]) {
        case 'A':
            string1 = "MON";
            break;
        case 'B':
            string1 = "TUE";
            break;
        case 'C':
            string1 = "WED";
            break;
        case 'D':
            string1 = "THU";
            break;
        case 'E':
            string1 = "FRI";
            break;
        case 'F':
            string1 = "SAT";
            break;
        case 'G':
            string1 = "SUN";
            break;
        default:
            break;
    }

    cout << string1 << " ";

    int string2;
    if (tc[1] - '0' < 10) {
        string2 = tc[1] - '0';
        cout << '0';
    } else {
        string2 = tc[1] - 'A' + 10;
    }

    cout << string2 << ":";

    if (temp < 10)
        cout << "0" << temp;
    else
        cout << temp;

    return 0;
}

1015. 德才論 (25)-PAT乙級真題

題目描述:
宋代史學家司馬光在《資治通鑑》中有一段著名的“德才論”:“是故才德全盡謂之聖人,才德兼亡謂之愚人,德勝才謂之君子,才勝德謂之小人。凡取人之術,苟不得聖人,君子而與之,與其得小人,不若得愚人。”
現給出一批考生的德才分數,請根據司馬光的理論給出錄取排名。
輸入格式:
輸入第1行給出3個正整數,分別爲:N(<=105),即考生總數;L(>=60),爲錄取最低分數線,即德分和才分均不低於L的考生纔有資格被考慮錄取;H(<100),爲優先錄取線——德分和才分均不低於此線的被定義爲“才德全盡”,此類考生按德才總分從高到低排序;才分不到但德分到線的一類考生屬於“德勝才”,也按總分排序,但排在第一類考生之後;德才分均低於H,但是德分不低於才分的考生屬於“才德兼亡”但尚有“德勝才”者,按總分排序,但排在第二類考生之後;其他達到最低線L的考生也按總分排序,但排在第三類考生之後。
隨後N行,每行給出一位考生的信息,包括:准考證號、德分、才分,其中准考證號爲8位整數,德才分爲區間[0, 100]內的整數。數字間以空格分隔。
輸出格式:
輸出第1行首先給出達到最低分數線的考生人數M,隨後M行,每行按照輸入格式輸出一位考生的信息,考生按輸入中說明的規則從高到低排序。當某類考生中有多人總分相同時,按其德分降序排列;若德分也並列,則按准考證號的升序輸出。
輸入樣例:
14 60 80
10000001 64 90
10000002 90 60
10000011 85 80
10000003 85 80
10000004 80 85
10000005 82 77
10000006 83 76
10000007 90 78
10000008 75 79
10000009 59 90
10000010 88 45
10000012 80 100
10000013 90 99
10000014 66 60
輸出樣例:
12
10000013 90 99
10000012 80 100
10000003 85 80
10000011 85 80
10000004 80 85
10000007 90 78
10000006 83 76
10000005 82 77
10000002 90 60
10000014 66 60
10000008 75 79
10000001 64 90
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

struct node {
    int num;
    int de;
    int cai;
};

bool cmp(node a, node b) {
    if ((a.de + a.cai) != (b.cai + b.de)) {
        return (a.de + a.cai) > (b.de + b.cai);
    } else if (a.de != b.de) {
        return a.de > b.de;
    } else {
        return a.num < b.num;
    }
}

int main() {
    int n, l, h;
    scanf("%d %d %d", &n, &l, &h);
    vector<node> vec1, vec2, vec3, vec4;
    node temp = {};
    int total = n;
    for (int i = 0; i < n; ++i) {
        scanf("%d %d %d", &temp.num, &temp.de, &temp.cai);
        if (temp.de < l || temp.cai < l) {
            total--;
            continue;
        }
        if (temp.de >= h && temp.cai >= h) {
            vec1.push_back(temp);
        }
        else if (temp.de >= h && temp.cai < h) {
            vec2.push_back(temp);
        }
        else if (temp.de < h && temp.cai < h && temp.de >= temp.cai) {
            vec3.push_back(temp);
        }
        else {
            vec4.push_back(temp);
        }
    }

    sort(vec1.begin(), vec1.end(), cmp);
    sort(vec2.begin(), vec2.end(), cmp);
    sort(vec3.begin(), vec3.end(), cmp);
    sort(vec4.begin(), vec4.end(), cmp);

    printf("%d\n", total);
    for (int j = 0; j < vec1.size(); j++) {
        printf("%d %d %d\n", vec1[j].num, vec1[j].de, vec1[j].cai);
    }
    for (int j = 0; j < vec2.size(); j++) {
        printf("%d %d %d\n", vec2[j].num, vec2[j].de, vec2[j].cai);
    }
    for (int j = 0; j < vec3.size(); j++) {
        printf("%d %d %d\n", vec3[j].num, vec3[j].de, vec3[j].cai);
    }
    for (int j = 0; j < vec4.size(); j++) {
        printf("%d %d %d\n", vec4[j].num, vec4[j].de, vec4[j].cai);
    }

    return 0;
}

1016. 部分A+B (15)-PAT乙級真題

正整數A的“DA(爲1位整數)部分”定義爲由A中所有DA組成的新整數PA。例如:給定A = 3862767,DA = 6,則A的“6部分”PA是66,因爲A中有2個6。
現給定A、DA、B、DB,請編寫程序計算PA + PB。
輸入格式:
輸入在一行中依次給出A、DA、B、DB,中間以空格分隔,其中0 < A, B < 10^10。
輸出格式:
在一行中輸出PA + PB的值。
輸入樣例1:
3862767 6 13530293 3
輸出樣例1:
399
輸入樣例2:
3862767 1 13530293 8
輸出樣例2:
0
#include <iostream>

using namespace std;

int main() {
    string a, b;
    int m, n;
    cin >> a >> m >> b >> n;
    int alen = a.length();
    int blen = b.length();
    int countA = 0;
    int countB = 0;
    for (int i = 0; i < alen; ++i) {
        if ((a[i] - '0') == m) {
            countA++;
        }
    }
    for (int i = 0; i < blen; ++i) {
        if ((b[i] - '0') == n) {
            countB++;
        }
    }

    int ca = 0;
    int cb = 0;
    if (countA != 0) {
        ca = m;
    }
    if (countB != 0) {
        cb = n;
    }
    for (int i = 1; i < countA; ++i) {
        ca = 10 * ca + m;
    }
    for (int i = 1; i < countB; ++i) {
        cb = cb * 10 + n;
    }

    cout << ca + cb;

    return 0;
}

1017. A除以B (20)-PAT乙級真題

本題要求計算A/B,其中A是不超過1000位的正整數,B是1位正整數。你需要輸出商數Q和餘數R,
使得A = B * Q + R成立。
輸入格式:
輸入在1行中依次給出A和B,中間以1空格分隔。
輸出格式:
在1行中依次輸出Q和R,中間以1空格分隔。
輸入樣例:
123456789050987654321 7
輸出樣例:
17636684150141093474 3
#include <iostream>

using namespace std;

int main() {
    string s;
    int a;
    cin >> s >> a;
    int len = s.length();
    int t = 0;
    int temp = 0;
    t = (s[0] - '0') / a;
    if ((t != 0 && len > 1) || len == 1) {
        cout << t;
    }
    temp = (s[0] - '0') % a;
    for (int i = 1; i < len; ++i) {
        t = (temp * 10 + s[i] - '0') / a;
        cout << t;
        temp = (temp * 10 + s[i] - '0') % a;
    }
    cout << " " << temp;

    return 0;
}

1001. A+B Format (20)-PAT甲級真題

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).

Input
Each input file contains one test case. Each case contains a pair of integers a and b where -1000000 <= a, b <= 1000000. The numbers are separated by a space.

Output
For each test case, you should output the sum of a and b in one line. The sum must be written in the standard format.

Sample Input
-1000000 9
Sample Output
-999,991
#include <iostream>

using namespace std;

int main() {
    int a, b;
    cin >> a >> b;
    string s = to_string(a + b);
    int len = s.length();
    for (int i = 0; i < len; ++i) {
        cout << s[i];
        if (s[i] == '-')
            continue;
        if ((i + 1) % 3 == len % 3 && i != len - 1)
            cout << ',';
    }

    return 0;
}

1002. A+B for Polynomials (25)-PAT甲級真題

This time, you are supposed to find A+B where A and B are two polynomials.

Input
Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial: K N1 aN1 N2 aN2 … NK aNK, where K is the number of nonzero terms in the polynomial, Ni and aNi (i=1, 2, …, K) are the exponents and coefficients, respectively. It is given that 1 <= K <= 10,0 <= NK < … < N2 < N1 <=1000.

Output
For each test case you should output the sum of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate to 1 decimal place.

Sample Input
2 1 2.4 0 3.2
2 2 1.5 1 0.5
Sample Output
3 2 1.5 1 2.9 0 3.2
#include <iostream>

using namespace std;

int main() {
    float c[1001] = {0};
    int k1, k2, t;
    float temp;
    cin >> k1;
    for (int i = 0; i < k1; ++i) {
        scanf("%d %f", &t, &temp);
        c[t] += temp;
    }

    cin >> k2;
    for (int i = 0; i < k2; ++i) {
        scanf("%d %f", &t, &temp);
        c[t] += temp;
    }

    int count = 0;
    for (int i = 0; i < 1001; ++i) {
        if (c[i] != 0)
            count++;
    }

    cout << count;
    if (count != 0)
        cout << " ";
    for (int i = 1000; i >= 0; --i) {
        if (c[i] != 0 && count > 0){
            printf("%d %.1f", i, c[i]);
            count--;
            if (count >= 1)
                printf(" ");
        }
    }

    return 0;
}

PAT 1070. 結繩(25)-乙級

給定一段一段的繩子,你需要把它們串成一條繩。每次串連的時候,是把兩段繩子對摺,再如下圖所示套接在一起。這樣得到的繩子又被當成是另一段繩子,可以再次對摺去跟另一段繩子串連。每次串連後,原來兩段繩子的長度就會減半。
給定N段繩子的長度,你需要找出它們能串成的繩子的最大長度。

輸入格式:

每個輸入包含1個測試用例。每個測試用例第1行給出正整數N (2 <= N <= 104);第2行給出N個正整數,即原始繩段的長度,數字間以空格分隔。所有整數都不超過104。

輸出格式:

在一行中輸出能夠串成的繩子的最大長度。結果向下取整,即取爲不超過最大長度的最近整數。

輸入樣例:
8
10 15 12 3 4 13 1 15
輸出樣例:
14
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
    int n;
    cin >> n;
    vector<int> vec(n);
    for (int i = 0; i < n; ++i) {
        cin >> vec[i];
    }
    sort(vec.begin(), vec.end());

    int result = vec[0];
    for (int i = 1; i < n; ++i) {
        result = (result + vec[i]) / 2;
    }

    cout << result;

    return 0;
}

L1-020. 帥到沒朋友-PAT團體程序設計天梯賽GPLT

當芸芸衆生忙着在朋友圈中發照片的時候,總有一些人因爲太帥而沒有朋友。本題就要求你找出那些帥到沒有朋友的人。
輸入格式:
輸入第一行給出一個正整數N(<=100),是已知朋友圈的個數;隨後N行,每行首先給出一個正整數K(<=1000),爲朋友圈中的人數,然後列出一個朋友圈內的所有人——爲方便起見,每人對應一個ID號,爲5位數字(從00000到99999),ID間以空格分隔;之後給出一個正整數M(<=10000),爲待查詢的人數;隨後一行中列出M個待查詢的ID,以空格分隔。
注意:沒有朋友的人可以是根本沒安裝“朋友圈”,也可以是隻有自己一個人在朋友圈的人。雖然有個別自戀狂會自己把自己反覆加進朋友圈,但題目保證所有K超過1的朋友圈裏都至少有2個不同的人。
輸出格式:
按輸入的順序輸出那些帥到沒朋友的人。ID間用1個空格分隔,行的首尾不得有多餘空格。如果沒有人太帥,則輸出“No one is handsome”。
注意:同一個人可以被查詢多次,但只輸出一次。
輸入樣例1:
3
3 11111 22222 55555
2 33333 44444
4 55555 66666 99999 77777
8
55555 44444 10000 88888 22222 11111 23333 88888
輸出樣例1:
10000 88888 23333
輸入樣例2:
3
3 11111 22222 55555
2 33333 44444
4 55555 66666 99999 77777
4
55555 44444 22222 11111
輸出樣例2:
No one is handsome
#include <iostream>
#include <set>
using namespace std;
int main() {
    int a, b, c;
    cin >> a;
    set<string> s, ans;
    string str;
    for(int i = 0; i < a; i++) {
        cin >> b;
        for(int j = 0; j < b; j++) {
            cin >> str;
            if (b >= 2)
                s.insert(str);
        }
    }

    cin >> c;
    int flag = 0;
    for(int i = 0; i < c; i++) {
        cin >> str;
        if(s.find(str) == s.end() && ans.find(str) == ans.end()) {
            ans.insert(str);
            if(flag == 1)
                cout << " ";
            cout << str;
            flag = 1;
        }
    }
    if(flag == 0)
        cout << "No one is handsome";
    return 0;
}

CCCC-GPLT L1-033. 出生年 天梯賽

以上是新浪微博中一奇葩貼:“我出生於1988年,直到25歲才遇到4個數字都不相同的年份。”也就是說,直到2013年才達到“4個數字都不相同”的要求。本題請你根據要求,自動填充“我出生於y年,直到x歲才遇到n個數字都不相同的年份”這句話。

輸入格式:
輸入在一行中給出出生年份y和目標年份中不同數字的個數n,其中y在[1, 3000]之間,n可以是2、或3、或4。注意不足4位的年份要在前面補零,例如公元1年被認爲是0001年,有2個不同的數字0和1。

輸出格式:
根據輸入,輸出x和能達到要求的年份。數字間以1個空格分隔,行首尾不得有多餘空格。年份要按4位輸出。注意:所謂“n個數字都不相同”是指不同的數字正好是n個。如“2013”被視爲滿足“4位數字都不同”的條件,但不被視爲滿足2位或3位數字不同的條件。

輸入樣例1:
1988 4
輸出樣例1:
25 2013
輸入樣例2:
1 2
輸出樣例2:
0 0001
#include <iostream>
#include <set>

using namespace std;
int main() {
    int y, n;
    cin >> y >> n;
    for (int i = y; i <= 3012; ++i) {
        set<int> s;
        int num = i;
        for (int j = 0; j < 4; ++j) {
            s.insert(num % 10);
            num /= 10;
        }

        if (s.size() == n) {
            printf("%d %04d", i - y, i);
            break;
        }
    }

    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章