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 ...
随机推荐
- 【未完成】[Spark SQL_2] 在 IDEA 中编写 Spark SQL 程序
0. 说明 在 IDEA 中编写 Spark SQL 程序,分别编写 Java 程序 & Scala 程序 1. 编写 Java 程序 待补充 2. 编写 Scala 程序 待补充
- CentOS7 中安装 MySQL
0. 说明 参考 centos7.2安装MySQL CentOS 7 下 Yum 安装 MySQL 5.7 两种方式安装 MySQL 安装 MySQL(yum) & 安装 MySQL(yum) ...
- Coursera-AndrewNg(吴恩达)机器学习笔记——第三周编程作业(逻辑回归)
一. 逻辑回归 1.背景:使用逻辑回归预测学生是否会被大学录取. 2.首先对数据进行可视化,代码如下: pos = find(y==); %找到通过学生的序号向量 neg = find(y==); % ...
- 代理工具--mitmproxy
#代理工具 mitmproxy 指令:mitmproxy -b ip -p port(代理ip设置为:ip,端口设置为:port) 拦截request: 输入字母“i”(代表Intercept fil ...
- beta冲刺————第三天(3/5)
完善的具体内容: 前端: (1)可以进行修改文字大小背景 其中,金色的文字个人觉得很好看,点赞.(我很满意啊) (2)可以改变成夜间模式(也很不错啊) 后端: 尝试将本地的后端war文件,以及数据库传 ...
- Android 7.0以上版本 系统解决拍照的问题 exposed beyond app through ClipData.Item.getUri()
解决方案1: android.os.FileUriExposedException: file:///storage/emulated/0/ilive/images/photophoto.jpeg e ...
- php 数据集转换树、递归重组节点信息多维数组(转)
一.将数据集转换成树 /** * 将返回的数据集转换成树 * @param array $list 数据集 * @param string $pk 主键 * @param string $pid 父节 ...
- Maven搭建私服
为什么要搭建私服?搭建私服有什么好处? 以我最近技术调研和相关的使用为起点概述: 首先说明,为什么要搭建私服? 搭建私服的目的是,通常企业项目开发,特别是使用maven作为项目管理,现在非常流行使用m ...
- VMware虚拟机下Linux系统的全屏显示
在VMware虚拟机下的Linux无法全屏的问题的解决方案如下: 1. 启动虚拟机,并启动Redhat6.4. 2. 点击“view”——然后将Autofit window这个选项勾选.(一般 ...
- 浅谈SDN架构下的运维工作
导读 目前国内的网络运维还处于初级阶段,工作人员每天就像救火一样,天天疲于奔命.运维人员只能埋头查找系统运行的日志,耗时耗力,老眼昏花不说,有时候忙了半天还一无所获,作为运维工程师的你,有木有遇到过类 ...