链接:https://www.patest.cn/contests/pat-a-practise/1001

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

Each input file contains one test case. Each case contains a pair of integers a and b where -1000000 <= a, b <= 1000000. The numbers are separated by a space.

Output

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

思路:使用C++中的stringstream来进行整数与字符串的转换,头文件为<sstream>,先直接进行数值计算,然后转化为字符串,使用string中的insert()函数来插入逗号,最好从后往前进行插入,对于负数需要注意一下,以免在负号和数值中插入了逗号。

#include<cstdio>
#include<string>
#include<iostream>
#include<sstream> using namespace std; int main(){
int a,b,c;
while(~scanf("%d%d",&a,&b)){
c=a+b;
stringstream ss;
ss<<c;
string ans;
ss>>ans;
for(int i=ans.length()-3;i>0;i-=3){
if(ans[i-1]=='-') continue;
ans.insert(i,",");
}
cout<<ans<<endl;
}
return 0;
}

  

PAT1001A+B Format的更多相关文章

  1. Spring resource bundle多语言,单引号format异常

    Spring resource bundle多语言,单引号format异常 前言 十一假期被通知出现大bug,然后发现是多语言翻译问题.法语中有很多单引号,单引号在format的时候出现无法匹配问题. ...

  2. c# 字符串连接使用“+”和string.format格式化两种方式

    参考文章:http://www.liangshunet.com/ca/201303/218815742.htm 字符串之间的连接常用的两种是:“+”连接.string.format格式化连接.Stri ...

  3. PAT甲级 1001. A+B Format (20)

    题目原文: Calculate a + b and output the sum in standard format -- that is, the digits must be separated ...

  4. Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define ...

    Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define ... 这个错误是因为有两个相 ...

  5. 【转】string.Format对C#字符串格式化

    转自:http://blog.csdn.net/samsone/article/details/7556781 1.格式化货币(跟系统的环境有关,中文系统默认格式化人民币,英文系统格式化美元) str ...

  6. VBA 格式化字符串 - Format大全

    VBA 格式化字符串 VBA 的 Format 函数与工作表函数 TEXT 用法基本相同,但功能更加强大,许多格式只能用于VBA 的 Format 函数,而不能用于工作表函数 TEXT ,以下是本人归 ...

  7. [Erlang 0111] Erlang Abstract Format , Part 2

       上回书,我们说到飞天玉虎蒋伯芳来到蜈蚣岭,不是,重来,上回咱们说到可以在Erlang Shell里面手工构造,加载并调用一个模块.在那个demo里面,我把多个Form单独生成出来,最后放在一起做 ...

  8. [Erlang 0110] Erlang Abstract Format , Part 1

    Erlang Abstract Format并不难懂,只是枯燥一点罢了,如果把Abstract Format的文档翻译出来,其实就是Erlang教科书中语法入门的部分. Erlang Abstract ...

  9. C#中string.format用法详解

    C#中string.format用法详解 本文实例总结了C#中string.format用法.分享给大家供大家参考.具体分析如下: String.Format 方法的几种定义: String.Form ...

随机推荐

  1. JS JQ 深拷贝之坑

    之前做留言板的时候,我就被深拷贝坑了一次,这次做API管理系统,没想到又被深拷贝坑了一次. 最后,拷贝对象的时候,如果要用到对象里的prototype,一定要用$.extend(true,{},要拷贝 ...

  2. 历史命令与实时记录(redhat6.8)

    历史命令与实时记录(redhat6.8) linuxshell 2018年02月13日 10时58分53秒 参数 HISTTIMEFORMAT HISTSIZE HISTFILESIZE HISTFI ...

  3. 虚拟机使用ceph-deploy安装ceph

    参考: ceph官网介绍 使用ceph-deploy安装Ceph 12.x 安装虚拟机 首先安装虚拟机环境,虚拟机安装这里不做介绍,本实验使用的镜像为CentOS-7-x86_64-Everythin ...

  4. 使用AES加密的勒索类软件分析报告

    报告名称:  某勒索类软件分析报告    作者:        李东 报告更新日期: 样本发现日期: 样本类型: 样本文件大小/被感染文件变化长度: 样本文件MD5 校验值: da4ab5e31793 ...

  5. js 原型原型链

    个人的理解,有什么不正确的请指教,共同学习 //声明一个构造函数Personfunction Person(name,age){ this.name = name; this.age = age;} ...

  6. SQL-53 按照dept_no进行汇总,属于同一个部门的emp_no按照逗号进行连接,结果给出dept_no以及连接出的结果employees

    题目描述 按照dept_no进行汇总,属于同一个部门的emp_no按照逗号进行连接,结果给出dept_no以及连接出的结果employeesCREATE TABLE `dept_emp` (`emp_ ...

  7. Oracle数据库表的一些宏处理

    比如现在,有个数据库表,我想要知道这个数据库已经建了多少张表?每个表有多少条数据?每个表都有哪些字段?以及字段的说明? 下面就用SQL一一解决上面的问题: --所有已存在的表名和说明 select t ...

  8. 用几句话说一说CMake add_dependencies & target_link_libraries的使用区别

    简单说一说前两天学习使用CMake解决链接问题时遇到的一个问题. 对于编译时遇到的依赖问题,很多时候我们只需要一句target_link_libraries就可以搞定. 但是CMake还有另外一个co ...

  9. vue-cli title 里面怎动态显示文字

    在路由里每个都添加一个meta[{ path:'/login', meta: { title: '登录页面' }, component:'login' }]   main.js里面加如下代码:     ...

  10. 主成分分析法PCA原理

    PCA(Principal Component Analysis)是一种常用的数据分析方法.PCA通过线性变换将原始数据变换为一组各维度线性无关的表示,可用于提取数据的主要特征分量,常用于高维数据的降 ...