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. 【洛谷】P1962

    题目链接:https://www.luogu.org/problemnew/show/P1962 题意:求fib数列的第n项,很大.mod 1e9+7. 题解:BM直接推. 代码: #include ...

  2. Tomcat调优详解

    前言 在这里告诫一下那些感觉自己啥都会的朋友们,其实你会的可能只是皮毛,不要感觉这个东西以前已经做过了,就不想去做了 其实你还远没有达到精通的地步,遇到以前做过的东西,也要用心的再去做一遍,你可能会从 ...

  3. iOS逆向系列-tweak补充

    tweak加载资源 开发自己的deb插件需要加载自己的资源,比如图片资源.iOS中常用的两种加载图片资源的方式: + (nullable UIImage *)imageNamed:(NSString ...

  4. 【模板篇】A* 寻路算法

    上次在做k短路的时候说到了A*, 但是并没有仔细的研究A*寻路, 毕竟k短路中的A*也不怎么标准… A*寻路的过程网上还是有很多的, 讲得也很清楚, 不妨跟着里面的图示自己动手操作一下, 基本一遍就能 ...

  5. android studio toolbar遮挡住下面控件内容

    只需要在该控件布局(content_***.xml)加入: app:layout_behavior="@string/appbar_scrolling_view_behavior" ...

  6. Java 基础 - java序列化 & serialVersionUID

    ref: https://www.cnblogs.com/duanxz/p/3511695.html ------------------- SerialVersionUID概述 SerialVers ...

  7. 【JZOJ3424】粉刷匠

    description 赫克托是一个魁梧的粉刷匠,而且非常喜欢思考= = 现在,神庙里有N根排列成一直线的石柱,从1到N标号,长老要求用油漆将这些石柱重新粉刷一遍.赫克托有K桶颜色各不相同的油漆,第i ...

  8. QueryList 来做采集

    示例代码 先来感受一下使用 QueryList 来做采集是什么样子. 1 采集百度搜索结果列表的标题和链接.大理石平台价格 采集代码: $data = QueryList::get('https:// ...

  9. dart中的typedef <函数别名>

    typedef定义如下: typedef 给某一种特定的函数类型起了一个名字,可以认为是一个类型的别名.或者这样理解: 自己定义了一种数据类型,不过这种数据类型是函数类型,按照这种类型实例化后的对象, ...

  10. vuecli脚手架+vue+vuex实现vue驱动的demo。

    哎呀呀呀,现在大家都要会Vue ||  React,否则感觉跟这个前端的世界脱节了一样. start: vue-cli这个构建工具大大降低了webpack的使用难度,支持热更新,有webpack-de ...