PAT甲级刷题记录

以下内容均参考自柳神——努力,自信,热爱生活的小姐姐。

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

Each input file contains one test case. Each case contains a pair of integers a and b where − 1 0 6 −10^ 6 106 ≤a,b≤ 1 0 6 10^6 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
思路:从第一个位置开始到当前下标位置的子串长度模3和整个串的长度模3相同的位置就是插入逗号的位置。

#include <iostream>
using namespace std;
	int main() {
	int a, b;
	cin >> a >> b;
	string s = to_string(a + b);
	int n = s.length();
	for (int i = 0; i < n; 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分)

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

Input Specification:

Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial:
K   N 1   a N 1 N 2   a N 2 . . .   N k   a N k K\space N_1\space a_{N_1} N_2\space a_{N_2}... \space N_k\space a_{N_k} K N1 aN1N2 aN2... Nk aNk
where K K K is the number of nonzero terms in the polynomial, N ​ i N_​i Ni and a ​ N ​ i a_{​N_​i} aNi ( i = 1 , 2 , ⋯ , K i=1,2,⋯,K i=1,2,,K) are the exponents and coefficients, respectively. It is given that 1 ≤ K ≤ 10 1≤K≤10 1K10 0 ≤ N K ​ ​ < ⋯ < N ​ 2 < N 1 ≤ 1000 0≤N_K​​ <⋯<N​_2<N_1 ≤1000 0NK<<N2<N11000.

Output Specification:

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
思路:直接开一个长度1001的浮点数组,下标代表指数,存放内容为系数。把读进来的数按照指数的位置来存放系数即可。

#include <stdio.h>
int main()
{
    float c[1001] = {0};
    int m, n, k, num = 0;
    float cc;
    scanf("%d", &m);
    for(int i = 0; i < m; i++) {
        scanf("%d%f", &k, &cc);
        c[k] += cc;
    }
    scanf("%d", &n);
    for(int i = 0; i < n; i++) {
        scanf("%d%f", &k, &cc);
        c[k] += cc;
    }
    for(int i = 0; i < 1001; i++) {
        if(c[i] != 0.0)
            num++;
    }
    printf("%d", num);
    for(int i = 1000; i > -1; i--) {
        if(c[i] != 0.0)
            printf(" %d %.1f", i, c[i]);
    }
    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 Specification:

Each input file contains one test case. For each test case, the first line contains 4 positive integers: N ( ≤ 500 ) N (≤500) N(500) - the number of cities (and the cities are numbered from 0 0 0 to N − 1 N−1 N1), M - the number of roads, C 1 C_1 C1 and C 2 C_2 C2 - the cities that you are currently in and that you must save, respectively. The next line contains N N N integers, where the i i i-th integer is the number of rescue teams in the i i i-th city. Then M M M lines follow, each describes a road with three integers c 1 c_1 c1, c 2 c_2 c2 and L L 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 C 1 C_1 C1 to C 2 C_2 C2.

Output Specification:

For each test case, print in one line two numbers: the number of different shortest paths between C 1 C_1 C1 and C 2 C_2 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
思路:迪杰斯特拉算法。

#include <iostream>
#include <algorithm>
#define INT_MAX 2147483647
using namespace std;

int n, m, c1, c2;
int e[500][500], w[500], dis[500], num[500], weight[500];
bool vis[500];

int main() {
    fill(e[0], e[0] + 500 * 500, INT_MAX);
    fill(dis, dis + 500, INT_MAX);
    int src, des, d;
    cin >> n >> m >> c1 >> c2;
    for(int i = 0; i < n; i++)
        cin >> weight[i];
    for(int i = 0; i < m; i++) {
        cin >> src >> des >> d;
        e[src][des] = e[des][src] = d;
    }
    dis[c1] = 0;
    w[c1] = weight[c1];
    num[c1] = 1;
    for(int i = 0; i < n; i++) {
        int minindex = -1, minvalue = INT_MAX;
        for(int j = 0; j < n; j++) {
            if(!vis[j] && dis[j] < minvalue) {
                minindex = j;
                minvalue = dis[j];
            }
        }
        if(minindex < 0)
            break;
        vis[minindex] = true;
        for(int j = 0; j < n; j++) {
            if(!vis[j] && e[minindex][j] < INT_MAX) {
                if(dis[minindex] + e[minindex][j] < dis[j]) {
                    dis[j] = dis[minindex] + e[minindex][j];
                    num[j] = num[minindex];
                    w[j] = w[minindex] + weight[j];
                }
                else if(dis[minindex] + e[minindex][j] == dis[j]) {
                    num[j] += num[minindex];
                    if(w[minindex] + weight[j] > w[j])
                        w[j] = w[minindex] + weight[j];
                }
            }
        }
    }
    cout << num[c2] << ' ' << w[c2];
    return 0;
}

1004 Counting Leaves (30分)

Input Specification:

Each input file contains one test case. Each case starts with a line containing 0 < N < 100 0<N<100 0<N<100, the number of nodes in a tree, and M ( < N ) M (<N) M(<N), the number of non-leaf nodes. Then M M 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 K 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 N 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
思路:数的遍历,深搜找叶子节点,每找到一片就把那层的叶子数+1

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> node[100];
int sum[89];
int deepest;
void dfs(int n, int depth) {
    if(node[n].size() == 0) {
        sum[depth]++;
        deepest = max(deepest, depth);
        return;
    }
    for(int i = 0; i < node[n].size(); i++)
        dfs(node[n][i], depth + 1);
}
int main() {
    int n, m, id, numChild, t;
    cin >> n >> m;
    for(int i = 0; i < m; i++) {
        cin >> id >> numChild;
        for(int j = 0; j < numChild; j++) {
            cin >> t;
            node[id].push_back(t);
        }
    }
    dfs(01, 0);
    cout << sum[0];
    for(int i = 1; i <= deepest; i++)
        cout << ' ' << sum[i];
    return 0;
}

1005 Spell It Right (20分)

Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output every digit of the sum in English.

Input Specification:

Each input file contains one test case. Each case occupies one line which contains an N ( ≤ 1 0 1 00 ) N (≤10^100 ) N(10100).

Output Specification:

For each test case, output in one line the digits of the sum in English words. There must be one space between two consecutive words, but no extra space at the end of a line.
12345

Sample Output:

one five
思路:这题很简单,但是要小心,数据的上限非常大,远非long long能存储的,所以应该直接用字符串来存储。

#include <iostream>
using namespace std;
int main() {
	string a;
	cin >> a;
	int sum = 0;
	for (int i = 0; i < a.length(); i++)
		sum += (a[i] - '0');
	string s = to_string(sum);
	string arr[10] = {"zero", "one", "two", "three", "four", "five", "six",
	"seven", "eight", "nine"};
	cout << arr[s[0] - '0'];
	for (int i = 1; i < s.length(); i++)
		cout << " " << arr[s[i] - '0'];
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值