PAT甲级题目1-10(C++)

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 $10^6​​a,b10​^6$​​. 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

解题思路:

首先看到问题里面a和b的值都只在$[-10^6,10^6]$,因此用整型就足够完成加法运算,因此这里可以直接保存结果为字符串result。接下来只需要倒序遍历字符串,每3个字符就插入1个逗号,需要注意的是,最后一组不能用逗号将符号和数字分离了,比如题目里的输出:$-999,991$,就不能写成$-,999,991$。因此需要加一个判断最开头的一位是否为符号即可。

Code:

 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 
 5 int main(){
 6     int a,b;
 7     cin>>a>>skipws>>b;
 8     string result = to_string(a+b);
 9     int number = 1;
10     int length = result.length();
11     string comma = ",";
12     for(int i = length-1;i>=1;i--){
13         if(number % 3 == 0 && isdigit(result[i-1])){
14             result = result.insert(i,comma);
15             number = 1;
16         }
17         else{
18             number++;
19         }
20     }
21     cout<<result<<endl;
22     return 0;
23 }

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}$

where $K$ is the number of nonzero terms in the polynomial, $N_i$ and $a_{N_i}(i = 1,2,…,K)$ are the exponents and coefficients, respectively. It is given that $1 leq K leq 10, 0 leq N_k < … < N_2 < N_1 leq 1000.$

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

解题思路:

这道题关键是要读懂题目里面说的是什么,是两个多项式相加,那就是次数不变,系数相加,根据给出的$K$的范围容易得出一开始要设定的数组长度。用数组索引作次数,数组存放的内容做系数,然后相加完以后再倒序遍历即可。

坑点:

测试点2和测试点5:这道题如果在(指数 系数)后面加空格的话,由于你不知道这个多项式的和的结果是否有常数项,如果没有常数项,那就会在Output中多了一个末尾的空格。因此要设置一个是否已经有输出的标识符,判断在输出(指数 系数)时前面是否需要加空格。

测试点6:$A+B=0$,对0要单独作额外的分类即可。

Code:

 1 #include<iostream>
 2 #include<string>
 3 #include<iomanip>
 4 using namespace std;
 5 double A[1001] = { 0 };
 6 double B[1001] = { 0 };
 7 int main() {
 8     int items;
 9     cin >> items >>skipws;
10     int times;
11     double coff;
12     for (int i = 0; i < items; i++) {
13         cin >> times >> skipws >> coff >> skipws;
14         A[times] = coff;
15     }
16     cin >> items>>skipws;
17     for (int i = 0; i < items; i++) {
18         cin >> times >> skipws >> coff >> skipws;
19         B[times] = coff;
20     }
21     int number = 0;
22     for (int i = 0; i < 1001; i++) {
23         A[i] += B[i];
24         if (A[i] != 0) {
25             number++;
26         }
27     }
28     if (number != 0) {
29         cout << number << " ";
30         bool has_been_output = false;
31         for (int i = 1001; i >= 1; i--) {
32             if (A[i] != 0) {
33                 if (!has_been_output) {
34                     cout << i << " " << fixed << setprecision(1) << A[i];
35                     has_been_output = true;
36                 }
37                 else {
38                     cout << " " << i << " " << fixed << setprecision(1) << A[i];
39                 }
40             }
41         }
42         if (A[0] != 0) {
43             cout << " " << 0 << " " << fixed << setprecision(1) << A[0] << endl;
44         }
45     }
46     else {
47         cout << number << endl;
48     }
49     return 0;
50 }

 (未完待续)

 

原文地址:https://www.cnblogs.com/jcchan/p/11502703.html