首页 > 技术文章 > PAT甲级

hxidkd 2017-11-14 00:24 原文

最近准备考一下PAT,感觉PAT甲级的题目比较适合练习快速ac能力,基本上看完每题都能有思路,但总不能一次过,需要好好练练。

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

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<bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
const int N = 1e5 + 5;
int main() {
    int a, b;
    cin >> a >> b;
    int c = a + b;
    if (c < 0) {
        c = -c;
        cout << "-";
    } else if (c == 0) {
        cout << "0\n";
        return 0;
    }
    vector<int> num;
    while (c) {
        num.push_back(c%10);
        c /= 10;
    }
    for (int i = num.size() - 1; i >= 0; i--) {
        cout << num[i];
        if (i % 3 == 0 && i) {
            cout << ",";
        }
    }
    cout << "\n";
    return 0;
}

思路二

将结果按1000进制存储,打印时printf(",%03d", ans[i])即可。

1002 A+B for Polynomials (25)

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

思路

用vector存储每项的系数然后输出即可。

#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
const int N = 1e5 + 5;
int main() {
    vector<double> nums(1001, 0);
    int k, n;
    double an;
    cin >> k;
    while (k--) {
        cin >> n >> an;
        nums[n] += an;
    }
    cin >> k;
    while (k--) {
        cin >> n >> an;
        nums[n] += an;
    }
    int sz = 0;
    for (double& num : nums) {
        if (abs(num) > 0.1) sz++;
    }
    printf("%d", sz);
    for (int i = 1000; i >= 0; i--) {
        if (abs(nums[i]) > 0.1)
            printf(" %d %.1lf", i, nums[i]);
    }
    cout << '\n';
    return 0;
}

1003. Emergency (25)

As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some other city, your job is to lead your men to the place as quickly as possible, and at the mean time, call up as many hands on the way as possible.

Input

Each input file contains one test case. For each test case, the first line contains 4 positive integers: N (<= 500) - the number of cities (and the cities are numbered from 0 to N-1), M - the number of roads, C1 and C2 - the cities that you are currently in and that you must save, respectively. The next line contains N integers, where the i-th integer is the number of rescue teams in the i-th city. Then M lines follow, each describes a road with three integers c1, c2 and L, which are the pair of cities connected by a road and the length of that road, respectively. It is guaranteed that there exists at least one path from C1 to C2.

Output

For each test case, print in one line two numbers: the number of different shortest paths between C1 and C2, and the maximum amount of rescue teams you can possibly gather.
All the numbers in a line must be separated by exactly one space, and there is no extra space allowed at the end of a line.

Sample Input
5 6 0 2
1 2 1 5 3
0 1 1
0 2 2
0 3 1
1 2 1
2 4 1
3 4 1
Sample Output
2 4

思路

dijsktra,加相等时的判断,用两个数组表示到达该点的最短路条数和最多的救援队总数。

#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> p;
const int inf = (~0u)>>1;
int n, m, c1, c2;
vector<p> g[500];
int dist[500];
int res[500];
int ressum[500] = {0};
int path[500] = {0};
void dij() {
    priority_queue<p, vector<p>, greater<p> > q;
    q.push({0, c1});
    path[c1] = 1;
    ressum[c1] = res[c1];
    while (!q.empty()) {
        int curnode = q.top().second;
        int curcost = q.top().first;
        q.pop();
        if (curcost > dist[curnode]) {
            continue;
        }
        for (int i = 0; i < g[curnode].size(); i++) {
            int nextnode = g[curnode][i].second;
            int nextcost = g[curnode][i].first;
            if (dist[nextnode] > curcost + nextcost) {
                dist[nextnode] = curcost + nextcost;
                ressum[nextnode] = res[nextnode] + ressum[curnode];
                path[nextnode] = path[curnode];
                q.push({dist[nextnode], nextnode});
            } else if (dist[nextnode] == curcost + nextcost) {
                ressum[nextnode] =  max(res[nextnode] + ressum[curnode], ressum[nextnode]);
                path[nextnode] += path[curnode];
            }
        }
    }
}
int main() {
    cin >> n >> m >> c1 >> c2;
    for (int i = 0; i < n; i++) {
        cin >> res[i];
    }
    fill(dist, dist + n, inf);
    int st, ed, d;
    while (m--) {
        cin >> st >> ed >> d;
        g[st].push_back({d, ed});
        g[ed].push_back({d, st});
    }
    dij();
    cout << path[c2] << " " << ressum[c2];
    return 0;
}

1004. Counting Leaves (30)

A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.

Input

Each input file contains one test case. Each case starts with a line containing 0 < N < 100, the number of nodes in a tree, and M (< N), the number of non-leaf nodes. Then M lines follow, each in the format:

ID K ID[1] ID[2] ... ID[K]
where ID is a two-digit number representing a given non-leaf node, K is the number of its children, followed by a sequence of two-digit ID's of its children. For the sake of simplicity, let us fix the root ID to be 01.

Output

For each test case, you are supposed to count those family members who have no child for every seniority level starting from the root. The numbers must be printed in a line, separated by a space, and there must be no extra space at the end of each line.

The sample case represents a tree with only 2 nodes, where 01 is the root and 02 is its only child. Hence on the root 01 level, there is 0 leaf node; and on the next level, there is 1 leaf node. Then we should output "0 1" in a line.

Sample Input
2 1
01 1 02
Sample Output
0 1

思路

感觉最后一题是最简单的,直接层序遍历就好。

#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> p;
const int inf = (~0u)>>1;
int main() {
    int n, m;
    cin >> n >> m;
    vector<int> child[100];
    while (m--) {
        string s;
        cin >> s;
        int k;
        cin >> k;
        int num = atoi(s.c_str());
        for (int i = 0; i < k; i++) {
            cin >> s;
            child[num].push_back(atoi(s.c_str()));
        }
    }
    queue<int> q;
    q.push(1);
    bool first = true;
    while (!q.empty()) {
        int sz = q.size();
        int ans = 0;
        for (int i = 0; i < sz; i++) {
            int cur = q.front();
            q.pop();
            if (child[cur].size() == 0) ans++;
            for (int j = 0; j < child[cur].size(); j++) {
                q.push(child[cur][j]);
            }
        }
        if (first) first = false;
        else cout << " ";
        cout << ans;
    }
    return 0;
}

PAT 1064 1099

二叉查找树
可利用中序遍历非递减 满二叉树父子节点之间关系快速建树

推荐阅读