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的更多相关文章

  1. PAT (Advanced Level) 1001. A+B Format (20)

    简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...

  2. 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 ...

  3. PTA (Advanced Level) 1004 Counting Leaves

    Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to count tho ...

  4. PTA(Advanced Level)1025.PAT Ranking

    To evaluate the performance of our first year CS majored students, we consider their grades of three ...

  5. 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 ...

  6. 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 ...

  7. 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. ...

  8. 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 ...

  9. PTA (Advanced Level) 1020 Tree Traversals

    Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. Given the ...

随机推荐

  1. 【概率论】条件概率 & 全概率公式 & 朴素贝叶斯公式

    0. 说明 条件概率 & 全概率公式 & 朴素贝叶斯公式 学习笔记 参考 scikit-learn机器学习(五)--条件概率,全概率和贝叶斯定理及python实现 1. 条件概率 [定 ...

  2. 7zip批量压缩,并批量改.jar

    批量压缩.bat--要和将要压缩的文件在同一级目录下 for /d %%X in (*) do "D:\Program Files\7-Zip\7z.exe" a "%% ...

  3. 高斯消去、追赶法 matlab

    1. 分别用Gauss消去法.列主元Gauss消去法.三角分解方法求解方程组 程序: (1)Guess消去法: function x=GaussXQByOrder(A,b) %Gauss消去法 N = ...

  4. beta冲刺————第二天(2/5)

    完善具体内容: 前端: (1)添加了更多设置 (2)点击后出现底栏,分别可以进行字体背景设置.收藏.分享等操作,同时可以看出对文章的排版进行了完善 后端: 对阿里云服务器中的环境进行配置,同时熟悉阿里 ...

  5. 阿里八八Alpha阶段Scrum(10/12)

    今日进度 叶文滔: 正在解决日程模块合并至主项目的问题 俞鋆: 完成了上传和下载头像的api,完善了登陆和注册的api 李嘉群: 正在尝试json文件的转化和发送请求 黄梅玲: 学习json数据解析和 ...

  6. Springboot整合log4j2日志全解

    目录 常用日志框架 日志门面slf4j 为什么选用log4j2 整合步骤 引入Jar包 配置文件 配置文件模版 配置参数简介 Log4j2配置详解 简单使用 使用lombok工具简化创建Logger类 ...

  7. input file 美化

    <input type='file'>的默认外观实在难看,绝大多数情况都需要对其美化.找了很多资料,目前发现以下方式是最简单的美化方式. 1.将file input用label包裹起来,然 ...

  8. python面试题(一)

    1.通过代码实现如下转换: 二进制转换成十进制:v = “0b1111011” #先将其转换为字符串,再使用int函数,指定进制转换为十进制. print(int("0b1111011&qu ...

  9. android 实现mqtt消息推送,以及不停断线重连的问题解决

    前段时间项目用到mqtt的消息推送,整理一下代码,代码的原型是网上找的,具体哪个地址已经忘记了. 代码的实现是新建了一个MyMqttService,全部功能都在里面实现,包括连服务器,断线重连,订阅消 ...

  10. OpenCV 中轮廓包裹的几个函数boundingRect、minAreaRect、minEnclosingCircle用法

    当我们得到对象轮廓后,可用boundingRect()得到包覆此轮廓的最小正矩形,minAreaRect()得到包覆轮廓的最小斜矩形,minEnclosingCircle()得到包覆此轮廓的最小圆形, ...