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. python中logging使用方法

    1.logging提供了一组便利的函数,用来做简单的日志.它们是 debug(). info(). warning(). error() 和 critical(). 1.1logging以严重程度递增 ...

  2. Java 基础 - Exception和Error

    综述 Exception 和 Error 都是继承了 Throwable 类,在 Java 中只有 Throwable 类型的实例才可以被抛出(throw)或者捕获(catch),它是异常处理机制的基 ...

  3. 14 win7 sp1下安装vs2015

    0 引言 在win7下安装vs2015的时候遇到了很多问题,看了很多帖子,尝试了很多次,终于成功了.网上也有大量关于win7 sp1下安装vs2015的帖子,我在安装的时候也参考了很多相关经验,这次写 ...

  4. http://wiki.ros.org/navigation/Tutorials/RobotSetup

    http://wiki.ros.org/navigation/Tutorials/RobotSetup

  5. vue框架介绍

    vue框架介绍 一.vue 概念 vue 是一种开发用户界面的渐进式开发框架.渐进式指的是:你可以将vue作为一部分嵌入到web应用中,带来丰富的交互体验 二.vue特点及常见开发中的高级功能 1.解 ...

  6. WordPress 3.8 中文正式版下载 - 建站与学习首选!全球最流行的开源PHP博客网站程序

    转载自:http://www.iplaysoft.com/wordpress.html 话说虽然我一直都在网站底部写着本站基于 WordPress 构建,但时常还是有人问我网站是用什么程序建的,还真有 ...

  7. css选择器之间的 空格和逗号

    当两个选择器之间有空格的情况下,代表的是子类选择器 .a .b{} 代表的是a类的b子类 而两个选择器之间没有空格的情况下,代表的是同时拥有两个类名的标签 <div class="a ...

  8. qfc 问题汇总(TO BE CONTINUED)

    硬件问题 增加一个复位按钮 程序问题 /* uart allocation: PB6-7: UART1 -> TELEM PD5-6 : UART2 -> SBUS PD8-9: UART ...

  9. USB电扇无刷电机改装

    现在USB电扇已经很常见了,网上随便可以低价买到.里面的电机分为有刷和无刷两种.我拆过的有刷USB电扇都非常劣质,里面的电机貌似是旧DVD机的拆机货:而无刷也有优劣之分,有的硅钢片非常少,铜线也细.这 ...

  10. POJ 1743-POJ - 3261~后缀数组关于最长字串问题

    POJ 1743 题意: 有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1~~88范围内的整数,现在要找一个重复的主题.“主题”是整个音符序列的一个子串,它需 ...