PAT 1001 A+B Format (20分) to_string()
题目
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 −106≤a,b≤106 . 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
题目解析
给出两个数字(-1000000
到1000000
之间),计算他们的和,以标准格式输出(形如 99,999,999
)
首先,两个数都是
-1000000
到1000000
之间,所以直接用int
保存求和即可,不会溢出然后为了输出方便,将其转为字符串,(
to_string()
是c++11引入的新方法)从前往后逐个输出字符,如果是负数,第一个字符是
'-'
什么时候要输出
','
,标准格式是从后往前三个一输出,假设转成字符串后的长度为len
,那么len % 3
就是最前面多出的长度,也就是第一个','
出现的位置,后面的都可以三个一组,就隔三个,输出一个','
。比如
12,345,666
,len = 8
,len % 3 = 2
,所以第2个数字后面加','
,第5个数字后面加 ',',第 8 个数字后加 ',',但是第8个是最后一个数字,所以要排除。所以 条件就是i % 3 == len % 3
,但是因为我们的下标是从0开始的,而我们是数数字个数判断,所以应该是(i + 1) % 3 == len % 3 && (i != len % 3)
代码
#include <iostream>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
// 两数和转为字符串
string s = to_string(a + b);
// 得到有效长度
int len = s.length();
for (int i = 0; i < len; i++) {
// 输出当前位
cout << s[i];
if (s[i] == '-')
continue;
// 标准化格式 -xx,123,999
if ((i + 1) % 3 == len % 3 && i != len - 1)
cout << ",";
}
return 0;
}
PAT 1001 A+B Format (20分) to_string()的更多相关文章
- PAT (Advanced Level) Practice 1001 A+B Format (20 分) 凌宸1642
PAT (Advanced Level) Practice 1001 A+B Format (20 分) 凌宸1642 题目描述: Calculate a+b and output the sum i ...
- PAT甲级——1001 A+B Format (20分)
Calculate a+b and output the sum in standard format – that is, the digits must be separated into gro ...
- PAT Advanced 1001 A+B Format (20 分)
Calculate a+b and output the sum in standard format -- that is, the digits must be separated into gr ...
- 【PAT甲级】1001 A+B Format (20 分)
题意:给两个整数a,b,计算a+b的值并每三位用逗号隔开输出(−1e6≤a,b≤1e6) AAAAAccepted code: #include<bits/stdc++.h> us ...
- PAT 1001 A+B Format (20 point(s))
题目: 我一开始的思路是: 用math.h中的log10函数来计算位数(不建议这么做,因为会很慢,而且会出一点别的问题): 用pow函数根据要插入分号的位置来拆分a+b成一个个数字(例如res / p ...
- PAT A1001 A+B Format (20 分)
AC代码 #include <cstdio> #include <algorithm> using namespace std; const int maxn = 11; in ...
- PAT 甲级 1001 A+B Format (20)(20 分)
1001 A+B Format (20)(20 分) Calculate a + b and output the sum in standard format -- that is, the dig ...
- PAT 甲级1001 A+B Format (20)(C++ -思路)
1001 A+B Format (20)(20 分) Calculate a + b and output the sum in standard format -- that is, the dig ...
- PAT甲 1001. A+B Format (20) 2016-09-09 22:47 25人阅读 评论(0) 收藏
1001. A+B Format (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Calculate ...
随机推荐
- LDA概率主题模型
目录 LDA 主题模型 几个重要分布 模型 Unigram model Mixture of unigrams model PLSA模型 LDA 怎么确定LDA的topic个数? 如何用主题模型解决推 ...
- 关于Swiper和vue数据顺序加载问题处理
在使用swiper插件的时候,常常因为异步加载数据产生的顺序问题而使插件不能正常实行,所以可以使用vue的updated来解决. 问:什么时候 进updated方法? 答:只有事先设置好的data变量 ...
- [Qt] Release模式下产生调试信息
分两步,设置Qt配置文件,设置VS. https://blog.csdn.net/itas109/article/details/83652387 F:\Qt\Qt5.7.1\5.7\msvc2015 ...
- 使用3种协议搭建本地yum仓库
关闭防火墙和selinux [root@qls yum.repos.d]# systemctl stop firewalld (stop,start,disable,enable) [root@qls ...
- Nginx SSL/HTTPS 配置
使用OpenSSL生成证书 1.生成RSA密钥的方法 openssl genrsa -des3 -out privkey.pem 2048 这个命令会生成一个2048位的密钥,同时有一个des3方法加 ...
- struts2验证码
验证码大多是jsp,servlet写的. 我拿来主义了, 再自己完善了一下(我一直努力想要站在巨人的肩膀) 首先是页面 test.jsp <%@ page contentType=" ...
- HTML(css 样式)
1.CSS 可以通过以下方式添加到 HTML 中: 内联样式 -- 在 HTML 元素中使用 "style" 属性 内部样式表 -- 在 HTML 文档头部 <head> ...
- Gitlab常规操作
一.Git和SVN的区别 和SVN类似,Git是一个版本控制系统(Version Control System,VCS),不同的是SVN为集中式版本控制系统,为单一的集中管理的服务器,保存所有文件的修 ...
- 最短路径树:Dijstra算法
一.背景 全文根据<算法-第四版>,Dijkstra算法.我们把问题抽象为2步:1.数据结构抽象 2.实现 二.算法分析 2.1 数据结构 顶点+边->图.注意:Dijkstra ...
- [LiDAR数据模拟]系列(1) HELIOS模拟平台介绍
关键词:LiDAR 激光雷达 点云模拟 作者:李二 日期:06/05/2020 - 07/05/2020 写在前面:我前段时间的一个工作(地基激光雷达TLS的新型布站策略)需要用到模拟的TLS点云数据 ...