PAT 甲级 1001 1004 题解

P1001 A+B Format

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 Specification:

Each input file contains one test case. Each case contains a pair of integers a and b where −106≤a,b≤106 . The numbers are separated by a space.

Output Specification:

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>

int main() {
    
    
    long long a, b;
    std::cin >> a >> b;
    long long c = a + b;
    if (c < 0) {
    
    
        std::cout << '-';
        c = -c;
    }
    auto str = std::to_string(c);
    auto len = str.length();
    auto pt = (len) % 3;
    for (int i = 0; i < pt; i ++) {
    
    
        std::cout << str[i];
    }
    for (int i = pt; i + 2 < len; i += 3) {
    
    
        if (pt == 0&& i ==pt )
            std::cout << str[i] << str[i + 1] << str[i + 2];
        else
            std::cout << "," << str[i] << str[i + 1] << str[i + 2];
    }

    return 0;
}

P1004 Counting Leaves

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

Input Specification:

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.

The input ends with N being 0. That case must NOT be processed.

Output Specification:

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

题解

题目的意思是让我们建立一颗树,我们明白题目的重点在于某节点是否有孩子以及是上一层哪个节点的孩子。题目让我们输出的答案是每一层没有孩子节点的个数,我们首先需要建立这棵树,然后知道这棵树有多少层,再然后如何去保存上下层孩子节点的关系,然后从上往下扫描并进行输出!
所以就根据输入建树即可,我这里用dfs建树

#include <iostream>
#include <string>
#include <set>
#include <map>
#include <vector>

using namespace std;
struct node {
    
    
    string id;
    set<int> children;
};
int deepx = -1;
vector<node *> nodeMap;
int levLeave[5000];

void dfs(int deep, struct node *n) {
    
    
    if (n == nullptr||n->children.empty()) {
    
    
        levLeave[deep]++;
        if (deepx < deep)deepx = deep;
        return;
    }
    for (auto it = n->children.begin(); it != n->children.end(); it++) {
    
    
        dfs(deep + 1, nodeMap[*it]);
    }
};

int main() {
    
    
    int N, M;
    cin >> N >> M;
    nodeMap.resize(M * N+100);
    struct node *root;
    for (int i = 0; i < M; ++i) {
    
    
        int id, k;
        cin >> id >> k;
        struct node *nud;

        nud = new struct node();
        nud->id = id;
        nodeMap[id] = nud;
        if (id == 1)
            root = nud;

        for (int j = 0; j < k; ++j) {
    
    
            int child;
            cin >> child;
            nud->children.insert(child);
        }
    }

    dfs(0, root);
    for (
            int i = 0;
            i < deepx;
            i++)
        cout << levLeave[i] << " ";


    cout << levLeave[deepx] <<
         endl;
    return 0;
}

Guess you like

Origin blog.csdn.net/m0_61974219/article/details/120373824