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,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++)的更多相关文章
- pat甲级题目1001 A+B Format详解
pat1001 A+B Format (20 分) Calculate a+b and output the sum in standard format -- that is, the digits ...
- PAT甲级考前整理(2019年3月备考)之一
转载请注明出处:https://www.cnblogs.com/jlyg/p/7525244.html 终于在考前,刷完PAT甲级131道题目,不容易!!!每天沉迷在刷题之中而不能超脱,也是一种 ...
- PAT甲级题分类汇编——序言
今天开个坑,分类整理PAT甲级题目(https://pintia.cn/problem-sets/994805342720868352/problems/type/7)中1051~1100部分.语言是 ...
- 2021.9.12周六PAT甲级考试复盘与总结
周六PAT甲级考试复盘与总结 先说结论:仍未步入"高手"行列:现在的学习节奏与方法是对的,有十万分的必要坚持下去. 题目 知识点 分数 T1 前缀和.二分 11 / 20 T2 排 ...
- 【PAT甲级】1070 Mooncake (25 分)(贪心水中水)
题意: 输入两个正整数N和M(存疑M是否为整数,N<=1000,M<=500)表示月饼的种数和市场对于月饼的最大需求,接着输入N个正整数表示某种月饼的库存,再输入N个正数表示某种月饼库存全 ...
- PAT甲级1131. Subway Map
PAT甲级1131. Subway Map 题意: 在大城市,地铁系统对访客总是看起来很复杂.给你一些感觉,下图显示了北京地铁的地图.现在你应该帮助人们掌握你的电脑技能!鉴于您的用户的起始位置,您的任 ...
- PAT甲级1014. Waiting in Line
PAT甲级1014. Waiting in Line 题意: 假设银行有N个窗口可以开放服务.窗前有一条黄线,将等候区分为两部分.客户要排队的规则是: 每个窗口前面的黄线内的空间足以包含与M个客户的一 ...
- PAT甲级考前整理(2019年3月备考)之三,持续更新中.....
PAT甲级考前整理一:https://www.cnblogs.com/jlyg/p/7525244.html,主要讲了131题的易错题及坑点 PAT甲级考前整理二:https://www.cnblog ...
- PAT甲级考前整理(2019年3月备考)之二,持续更新中.....
PAT甲级考前整理之一网址:https://www.cnblogs.com/jlyg/p/7525244.html,主要总结了前面131题的类型以及易错题及坑点. PAT甲级考前整理三网址:https ...
随机推荐
- jquery与zend framework编写的联动选项效果
html部分: <pre name="code" class="html"><!DOCTYPE html PUBLIC "-//W3 ...
- 了解JNI技术
在线文库系统用到了Jacob,它是基于JNI技术实现的,通过调用MS Office的Com接口实现,我这里对JNI技术进行一个学习. 基本上JNI技术相当于一个代理,因为java的跨平台特性,所以它自 ...
- Python3批量修改指定目录下面的图片/文件名
需求: 从网上下载的N张.png图片保存到image目录中,将下载下来的图片全部重命名test1.png/test2.png... 实现代码: 目录结构: config-->setting.py ...
- note : Get FilePathName from FILE_OBJECT
转自:http://blog.csdn.net/lostspeed/article/details/11738311 封了一个函数, 从 FILE_OBJECT 中 得到 FilePathName 在 ...
- 深度探索C++对象模型之第二章:构造函数语意学之成员初始值列表
当我们需要设置class member的初值时,要么是经过member initialization list ,要么在construcotr内. 一.先讨论必须使用member initializa ...
- Mysql查漏补缺
Mysql查漏补缺 存储引擎 数据库使用存储引擎来进行CRUD的操作,不同的存储引擎提供了不同的功能.Mysql支持的存储引擎有InnoDB.MyISAM.Memory.Merge.Archive.F ...
- linux zip,tar压缩文件夹 忽略 .git 文件夾
linux zip 忽略 .git 文件夾 # zip 命令 zip -r bitvolution.zip bitvolution -x *.git* # tar命令压缩文件夹忽略 .git文件夹 t ...
- undertow服务器
参考地址:http://undertow.io/undertow-docs/undertow-docs-1.3.0/index.html 1.引入相关jar <dependencies> ...
- C++ 贪吃蛇一维
#include <iostream> #include <conio.h> #include <windows.h> #include <time.h> ...
- thinkphp url生成
为了配合所使用的URL模式,我们需要能够动态的根据当前的URL设置生成对应的URL地址,为此,ThinkPHP内置提供了U方法,用于URL的动态生成,可以确保项目在移植过程中不受环境的影响. 定义规则 ...