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 ...
随机推荐
- springboot解决文件上传大小限制
(1)在配置文件(application.properties)加入如下代码 springboot2.0以下配置为: spring.http.multipart.maxFileSize = 10Mb ...
- ubuntu google chrome 全屏显示命令
全屏模式:kiosk默认全屏打开一个网页呢,只需要在快捷方式中加上 --kiosk [url] 就可以了.终端命令行打开: google-chrome --kiosk www.baidu.com 参考 ...
- sprindmvc
Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职责解耦,基于请求驱动指的就是使用请求-响应模 ...
- Hadoop 搭建集群的步骤
1.安装jdk,配置环境变量 root@localhost java]# vi /etc/profile 在profile中添加如下内容: #set java environmentexport J ...
- 【原创】使用开源libimobiledevice盗取iphone信息
一.概述 libimobiledevice可以理解为Linux系统下的iTunes,破解了iTunes的通信协议. 依赖:https://github.com/libimobiledevic ...
- mysql导出导入数据无权限
问题:The MySQL server is running with the --secure-file-priv option so it cannot execute this statemen ...
- 爬虫豆瓣top250项目-开发文档
项目托管平台地址:https://github.com/gengwenhao/GetTop250.git 负责内容:1.使用python的request库先获取网页内容下来 2.再使用一个好用的lxm ...
- 运维yum搭建zabbix
前言: zabbix([`zæbiks])是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案. zabbix能监视各种网络参数,保证 ...
- Linux虚拟机部署单机solr报错500解决方法之一
HTTP Status 500 - {msg=SolrCore 'collection1' is not available due to init failure: Could not load c ...
- Python 多线程的程序不结束多进程的程序不结束的区别
import time from threading import Thread from multiprocessing import Process #守护进程:主进程代码执行运行结束,守护进程随 ...