链接: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. java打印系统时间

    public class Time { public static void main(String[] args) { Date t = new Date(); DateFormat ti = ne ...

  2. git 仓库原理

    Git 版本控制原理 标签: git 版本控制 版本回退 2017年01月13日 21:07:202399人阅读 评论(0) 收藏 举报  分类: Git(4)  版权声明:本文为博主原创文章,未经博 ...

  3. 【算法】单源最短路——Dijkstra

    对于固定起点的最短路算法,我们称之为单源最短路算法.单源最短路算法很多,最常见的就是dijkstra算法. dijkstra主要用的是一种贪心的思想,就是说如果i...s...t...j是最短路,那么 ...

  4. windows 10系统在右键中添加管理员打开cmd

    需要修改注册表内容,新建文件,后缀名改为reg,文件中粘贴下边的代码 Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\Directory ...

  5. .NET快速开发平台免费版预发布

    自己团队开发的一套软件,可通过配置完成列表.表单.流程等的快速开发,因项目原因有一段时间没更新了,准备发出来希望能帮助更多企业快速实现信息化. 该软件主要应用的技术有如下: 1.存储:采用SqlSer ...

  6. 关于解决Mac使用docker安装SQL server for Linux 中文乱码问题

    本人是Mac的追随者,无奈本学期数据库课要求使用Microsoft的SQL server.但是Microsoft并没有发布SQL server for Mac ,笔者使用Google搜索后, 发现可以 ...

  7. java——形参与实参

    看了很多的文章,稍微有一些的总结:对最基本的形参与实参有了一定的理解,虽然还是不够深入. 1.基本概念 形参:全称为"形式参数"是在定义函数名和函数体的时候使用的参数,目的是用来接 ...

  8. 自己动手写CPU——寄存器堆、数据存储器(基于FPGA与Verilog)

    上一篇写的是基本的设计方案,由于考研复习很忙,不知道下一次什么时候才能打开博客,今天就再写一篇.写一写CPU中涉及到RAM的部件,如寄存器堆.数据存储器等. 大家应该在大一刚接触到计算机的时候就知道R ...

  9. js递归算法1+ 2+3.....100的和

    function num(n){    if(n==1) return 1;    return num(n-1)+n;}num(100);

  10. 新版ios证书的申请

    现在IOS不再开放提供测试证书了,整理一个申请证书的流程. 1. 申请应用的id 链接地址 https://developer.apple.com/登陆开发者中心,在account界面点击红框里面得区 ...