PAT1001A+B Format
链接:https://www.patest.cn/contests/pat-a-practise/1001
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的更多相关文章
- Spring resource bundle多语言,单引号format异常
Spring resource bundle多语言,单引号format异常 前言 十一假期被通知出现大bug,然后发现是多语言翻译问题.法语中有很多单引号,单引号在format的时候出现无法匹配问题. ...
- c# 字符串连接使用“+”和string.format格式化两种方式
参考文章:http://www.liangshunet.com/ca/201303/218815742.htm 字符串之间的连接常用的两种是:“+”连接.string.format格式化连接.Stri ...
- PAT甲级 1001. A+B Format (20)
题目原文: Calculate a + b and output the sum in standard format -- that is, the digits must be separated ...
- 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 ... 这个错误是因为有两个相 ...
- 【转】string.Format对C#字符串格式化
转自:http://blog.csdn.net/samsone/article/details/7556781 1.格式化货币(跟系统的环境有关,中文系统默认格式化人民币,英文系统格式化美元) str ...
- VBA 格式化字符串 - Format大全
VBA 格式化字符串 VBA 的 Format 函数与工作表函数 TEXT 用法基本相同,但功能更加强大,许多格式只能用于VBA 的 Format 函数,而不能用于工作表函数 TEXT ,以下是本人归 ...
- [Erlang 0111] Erlang Abstract Format , Part 2
上回书,我们说到飞天玉虎蒋伯芳来到蜈蚣岭,不是,重来,上回咱们说到可以在Erlang Shell里面手工构造,加载并调用一个模块.在那个demo里面,我把多个Form单独生成出来,最后放在一起做 ...
- [Erlang 0110] Erlang Abstract Format , Part 1
Erlang Abstract Format并不难懂,只是枯燥一点罢了,如果把Abstract Format的文档翻译出来,其实就是Erlang教科书中语法入门的部分. Erlang Abstract ...
- C#中string.format用法详解
C#中string.format用法详解 本文实例总结了C#中string.format用法.分享给大家供大家参考.具体分析如下: String.Format 方法的几种定义: String.Form ...
随机推荐
- CString与string、char*的区别和转换
转自:http://blog.csdn.net/luoweifu/article/details/20232379 我们在C++的开发中经常会碰到string.char*以及CString,这三种都表 ...
- ansible-play中role的基本用法
#role应用 #roles跟调用角色的剧本文件应该与roles同级关系,即放在ansible目录下 #makir /root/ansible/roles/{nginx,http,ftp,mysql, ...
- DevExpress VCL Controls 2019发展路线图(No.3)
[DevExpress VCL Controls下载] ExpressFlowChart 允许最终用户修改形状(v19.1) 允许开发人员以XML格式定义自定义形状(v19.1) 使用30多个新形状扩 ...
- servlet中常用到的工具
1. 解析ajax传来的json字符串,得到json对象 private JSONObject getJsonObject(HttpServletRequest req) { StringBuffer ...
- 团队作业3——alpha阶段冲刺
1.博客链接集合 敏捷冲刺1 敏捷冲刺2 敏捷冲刺3 敏捷冲刺4 敏捷冲刺5 敏捷冲刺6 敏捷冲刺7 测试随笔 冲刺总结随笔 2.项目git地址 git地址 3.项目成员贡献比例 成员 比例 张艺琳( ...
- linux小计
一.查看远程主机开放端口命令 nc -zv 10.10.79.89 5151 二.远程登录ssh/scp yum install sshpass sshpass -p youpassword scp ...
- 如何使用HTML5的WebSocket实现网页与服务器的双工通信(一)
本系列服务端双工通信包括两种实现方式:一.使用Socket构建:二.使用WCF构建.本文为使用Socket构建服务端的双工通信,客户端同样使用Html5的WebSocket技术进行调用. 一.网页客户 ...
- javaEE练习(商城练习)
今天写一个商城的练习,综合之前学习过的servlet和el表达式,来一个综合的练习: 需要用到的数据库有: /* Navicat MySQL Data Transfer Source Server : ...
- Maven可以使用mvn package指令对项目进行打包,如果使用Java -jar xxx.java
Maven可以使用mvn package指令对项目进行打包,如果使用Java -jar xxx.jar执行运行jar文件,会出现"no main manifest attribute, in ...
- HML
1.元素分类: 2.浮动: 在非IE浏览器(如Firefox)下,当容器的高度为auto,且容器的内容中有浮动(float为left或right)的元素,在这种情况下,容器的高度不能自动伸长以适应内容 ...