PTA (Advanced Level) 1001 A+B Format
1001 A+B Format
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 −. 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
题目解析
本题给出两个正整数,要求将其和由最后一位的数字开始每隔三位加一个,但是首尾与负数的符号后第一位都不可以有,
可以使用头文件string中的to_string()函数将两数加和转化为字符串。
将其反转,反转后首位便是和的最后一位,遍历反转后字符串,将其加入ans,每隔3位向ans中添加一位 , 对末位与符号前位进行特判即可。
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a, b;
scanf("%d%d", &a, &b); //输入a b
string str = to_string(a + b);
//将a与b的和转换为字符串储存在str中
int cnt = ;
string ans = "";
//ans记录最终答案
reverse(str.begin(),str.end());
//由于要从最后一个数字开始每隔三个数字添加一个 ,
//将str反转
for(auto i : str){
ans += i;
//每隔3个数字添加一个 , 最后一个位置与-前的位置不能添加 ,
if(cnt % == && cnt != str.size() && !(cnt == str.size() - && str[str.size() - ] == '-'))
ans += ',';
cnt++;
}
reverse(ans.begin(),ans.end());
cout << ans << endl;
return ;
}
PTA (Advanced Level) 1001 A+B Format的更多相关文章
- PAT (Advanced Level) 1001. A+B Format (20)
简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...
- PTA(Advanced Level)1036.Boys vs Girls
This time you are asked to tell the difference between the lowest grade of all the male students and ...
- PTA (Advanced Level) 1004 Counting Leaves
Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to count tho ...
- PTA(Advanced Level)1025.PAT Ranking
To evaluate the performance of our first year CS majored students, we consider their grades of three ...
- PTA (Advanced Level) 1009 Product of Polynomials
1009 Product of Polynomials This time, you are supposed to find A×B where A and B are two polynomial ...
- PTA (Advanced Level) 1006 Sign In and Sign Out
Sign In and Sign Out At the beginning of every day, the first person who signs in the computer room ...
- PTA (Advanced Level) 1002 A+B for Polynomials
1002 A+B for Polynomials This time, you are supposed to find A+B where A and B are two polynomials. ...
- PTA (Advanced Level) 1027 Colors in Mars
Colors in Mars People in Mars represent the colors in their computers in a similar way as the Earth ...
- PTA (Advanced Level) 1020 Tree Traversals
Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. Given the ...
随机推荐
- 【Weex学习】环境搭建
教程来源:http://jspang.com/2017/07/12/weex/,我本地是第一次安装Android Studio和教程有些出入 一.软件安装 1.安装Node.js 2.安装Java(h ...
- Linux运维之——每日小技巧,使用awk命令截取每行的指定列数据
获取/etc/passwd目录下的UID值小于10的数,并输出第一.三列 [root@:vg_adn_tidbCkhsTest:172.31.30.62 ~]#cat /etc/passwd | aw ...
- String、StringBuffer与StringBuilder的区别-陈远波
String Stringbuffer StringBuilder的区别: 1.string是字符串常量,且长度是不可改变的,Stringbuffer.stringBuilder是字符串变量 2.S ...
- input file 美化
<input type='file'>的默认外观实在难看,绝大多数情况都需要对其美化.找了很多资料,目前发现以下方式是最简单的美化方式. 1.将file input用label包裹起来,然 ...
- Nodejs学习资源汇总
Node.js v6.3.1 Documentation https://nodejs.org/dist/latest-v6.x/docs/api/ npm官网 https://www.npmjs ...
- redis集群搭建及连接(阿里云)
阿里云上面装redis集群基本被虐死,主要问题就是私有IP和公有IP. 下面分享成功搭建的步骤: 两台测试服务器,分别为:127.0.0.1,127.0.0.2.每分服务器有3个节点. 1.127.0 ...
- Ubuntu安装Chromium浏览器
今天介绍一下谷歌浏览器在ubuntu 系统环境下的安装步骤,1.在终端的窗口上输入: sudo wget http://www.linuxidc.com/files/repo/google-chrom ...
- Java中的单利模式介绍
单利模式:本来是不准备写的,但是最近发现好多公司面试时都会或多或少的提到单利模式,因此今天把单利模式拉出来说说. 定义:只包含一个被称为单例类的特殊类.通过单例模式可以保证系统中一个类只有一个实例而且 ...
- 蓝桥杯 历届试题 网络寻路(dfs搜索合法路径计数)
X 国的一个网络使用若干条线路连接若干个节点.节点间的通信是双向的.某重要数据包,为了安全起见,必须恰好被转发两次到达目的地.该包可能在任意一个节点产生,我们需要知道该网络中一共有多少种不同的转发路径 ...
- mysql中唯一约束用法
以前比较naive,有次同事一定要在表里建唯一约束的时候,我就很纳闷为啥非要在db层面做限制,在自己的业务代码里做啊,就是说入库的时候先查一遍有没有,没有记录的情况再准许入库. 后来发现如果只是自己处 ...