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,b≤10​^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:

- 

Sample Output:

-,

解题思路:

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

Code:

 #include<iostream>
#include<string>
using namespace std; int main(){
int a,b;
cin>>a>>skipws>>b;
string result = to_string(a+b);
int number = ;
int length = result.length();
string comma = ",";
for(int i = length-;i>=;i--){
if(number % == && isdigit(result[i-])){
result = result.insert(i,comma);
number = ;
}
else{
number++;
}
}
cout<<result<<endl;
return ;
}

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.4  3.2
1.5 0.5

Sample Output:

  1.5  2.9  3.2

解题思路:

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

坑点:

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

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

Code:

 #include<iostream>
#include<string>
#include<iomanip>
using namespace std;
double A[] = { };
double B[] = { };
int main() {
int items;
cin >> items >>skipws;
int times;
double coff;
for (int i = ; i < items; i++) {
cin >> times >> skipws >> coff >> skipws;
A[times] = coff;
}
cin >> items>>skipws;
for (int i = ; i < items; i++) {
cin >> times >> skipws >> coff >> skipws;
B[times] = coff;
}
int number = ;
for (int i = ; i < ; i++) {
A[i] += B[i];
if (A[i] != ) {
number++;
}
}
if (number != ) {
cout << number << " ";
bool has_been_output = false;
for (int i = ; i >= ; i--) {
if (A[i] != ) {
if (!has_been_output) {
cout << i << " " << fixed << setprecision() << A[i];
has_been_output = true;
}
else {
cout << " " << i << " " << fixed << setprecision() << A[i];
}
}
}
if (A[] != ) {
cout << " " << << " " << fixed << setprecision() << A[] << endl;
}
}
else {
cout << number << endl;
}
return ;
}

(未完待续)

PAT甲级题目1-10(C++)的更多相关文章

  1. pat甲级题目1001 A+B Format详解

    pat1001 A+B Format (20 分) Calculate a+b and output the sum in standard format -- that is, the digits ...

  2. PAT甲级考前整理(2019年3月备考)之一

       转载请注明出处:https://www.cnblogs.com/jlyg/p/7525244.html 终于在考前,刷完PAT甲级131道题目,不容易!!!每天沉迷在刷题之中而不能超脱,也是一种 ...

  3. PAT甲级题分类汇编——序言

    今天开个坑,分类整理PAT甲级题目(https://pintia.cn/problem-sets/994805342720868352/problems/type/7)中1051~1100部分.语言是 ...

  4. 2021.9.12周六PAT甲级考试复盘与总结

    周六PAT甲级考试复盘与总结 先说结论:仍未步入"高手"行列:现在的学习节奏与方法是对的,有十万分的必要坚持下去. 题目 知识点 分数 T1 前缀和.二分 11 / 20 T2 排 ...

  5. 【PAT甲级】1070 Mooncake (25 分)(贪心水中水)

    题意: 输入两个正整数N和M(存疑M是否为整数,N<=1000,M<=500)表示月饼的种数和市场对于月饼的最大需求,接着输入N个正整数表示某种月饼的库存,再输入N个正数表示某种月饼库存全 ...

  6. PAT甲级1131. Subway Map

    PAT甲级1131. Subway Map 题意: 在大城市,地铁系统对访客总是看起来很复杂.给你一些感觉,下图显示了北京地铁的地图.现在你应该帮助人们掌握你的电脑技能!鉴于您的用户的起始位置,您的任 ...

  7. PAT甲级1014. Waiting in Line

    PAT甲级1014. Waiting in Line 题意: 假设银行有N个窗口可以开放服务.窗前有一条黄线,将等候区分为两部分.客户要排队的规则是: 每个窗口前面的黄线内的空间足以包含与M个客户的一 ...

  8. PAT甲级考前整理(2019年3月备考)之三,持续更新中.....

    PAT甲级考前整理一:https://www.cnblogs.com/jlyg/p/7525244.html,主要讲了131题的易错题及坑点 PAT甲级考前整理二:https://www.cnblog ...

  9. PAT甲级考前整理(2019年3月备考)之二,持续更新中.....

    PAT甲级考前整理之一网址:https://www.cnblogs.com/jlyg/p/7525244.html,主要总结了前面131题的类型以及易错题及坑点. PAT甲级考前整理三网址:https ...

随机推荐

  1. 使Excel中单元格内英文为Arial Narrow 中文为宋体显示打印

    因为在对数据表进行排版格式整理的时候,发现Arial Narrow字体是不支持中文的,所以中文默认为宋体,但是显示出来却不是宋体,需要双击单元格中文才显示为宋体,这样打印出来才为宋体 但是如果有很多单 ...

  2. Django ORM 之 单表、多表查询

    返回ORM目录 Django ORM Django ORM 之一 内容目录: 一.单表查询 二.多表查询 0.准备工作 一些说明: - 表myapp_person的名称是自动生成的,如果你要自定义表名 ...

  3. Font Awesome (Mark)

    Font Awesome为您提供可缩放的矢量图标,您可以使用CSS所提供的所有特性对它们进行更改,包括:大小.颜色.阴影或者其它任何支持的效果. 一个字库,675个图标 仅一个Font Awesome ...

  4. leetcode-50-pow()

    题目描述: 方法一: class Solution: def myPow(self, x: float, n: int) -> float: if n<0: x = 1/x return ...

  5. linux dmesg命令使用

    linux dmesg命令使用 2012-11-27 09:37 2783人阅读 评论(0) 收藏 举报  分类: linux内核与编程(199)  版权声明:本文为博主原创文章,未经博主允许不得转载 ...

  6. QQ空间批量删除说说

    按下F12,贴下如下代码 var delay = 1000; function del() { if (document.querySelector(".app_canvas_frame&q ...

  7. 2019亚太内容分发大会,阿里云获CDN领袖奖、技术突破奖

    近日,亚太CDN产业联盟主办的2019亚太内容分发大会在上海召开.本次大会以"5G分发"为主题,集结了CDN领域近千名行业领袖.专家参与.在会上,阿里云斩获“CDN领袖奖”.“技术 ...

  8. C++——多态

    1. 多态定义的构成条件 多态是在不同继承关系的类对象,去调同一函数,产生了不同的行为. 就是说,有一对继承关系的两个类,这两个类里面都有一个函数且名字.参数.返回值均相同,然后我们通过调用函数来实现 ...

  9. Linux unzip解压多个文件

    假设当前目录下有多个zip文件 data.zip invoices.zip pictures.zip visit.zip, 直接 unzip *.zip 等价于 unzip data.zip invo ...

  10. 伪造ip头

    x-forwarded-for: 127.0.0.1x-remote-IP: 127.0.0.1x-remote-ip: 127.0.0.1x-client-ip: 127.0.0.1x-client ...