题目

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≤10​6​​ . 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

题目解析

给出两个数字(-10000001000000之间),计算他们的和,以标准格式输出(形如 99,999,999

  • 首先,两个数都是-10000001000000之间,所以直接用int保存求和即可,不会溢出

  • 然后为了输出方便,将其转为字符串,(to_string()是c++11引入的新方法)

  • 从前往后逐个输出字符,如果是负数,第一个字符是 '-'

  • 什么时候要输出 ',' ,标准格式是从后往前三个一输出,假设转成字符串后的长度为len,那么 len % 3 就是最前面多出的长度,也就是第一个 ',' 出现的位置,后面的都可以三个一组,就隔三个,输出一个 ','

    比如 12,345,666len = 8len % 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()的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 【PAT甲级】1001 A+B Format (20 分)

    题意:给两个整数a,b,计算a+b的值并每三位用逗号隔开输出(−1e6​​≤a,b≤1e6​​) AAAAAccepted code: #include<bits/stdc++.h> us ...

  5. PAT 1001 A+B Format (20 point(s))

    题目: 我一开始的思路是: 用math.h中的log10函数来计算位数(不建议这么做,因为会很慢,而且会出一点别的问题): 用pow函数根据要插入分号的位置来拆分a+b成一个个数字(例如res / p ...

  6. PAT A1001 A+B Format (20 分)

    AC代码 #include <cstdio> #include <algorithm> using namespace std; const int maxn = 11; in ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. Java集合案例(产生不重复随机数)

    获取10个1-20之间的随机数,要求不能重复 用数组实现,但是数组的长度是固定的,长度不好确定.所以我们使用集合实现. 分析:A:创建产生随机数的对象B:创建一个存储随机数的集合C:定义一个统计变量. ...

  2. java 容器(collection)--ArrayList 常用方法分析 源码分析

    ArrayList 介绍 打开jdk源码看看官方文档的介绍 粗糙的翻译下大致意思是: List接口的可调整大小的数组实现.实现了所有可选的列表操作,并允许所有元素,包括 null .除了实现List接 ...

  3. Kubernetes笔记(一):十分钟部署一套K8s环境

    Kubernetes是Goole开源的一个容器编排引擎,它支持自动化部署.大规模可伸缩.应用容器化管理 -- 百度百科. 接触K8s也有半年多了,也基于阿里云平台搭建了包含多级服务.目前运行较为稳定的 ...

  4. phpstorm破解版

    查看下载:https://www.7down.com/soft/229568.html 破解:https://www.7down.com/article/305640.html 主题更换和下载:htt ...

  5. 微信小程序弹出层动画特效

    .rules_modal_cont{ height:800rpx; width:200rpx; -webkit-animation: showZeroAlert .3s; animation: sho ...

  6. 走 进 java 的 四 个 基 本 特 性

    赶上明天就还是五一c小长假了,准备在这几天写几篇原创文章,供大家一起学习. 首先今天就来好好地唠一唠,到底java的那几个特性都是什么呢?到底怎么用呢?相信一定有一些小白对此会有些懊恼,没关系的,谁还 ...

  7. 第三方库PyYAML

    建议参考PyYAML Documentation来源:http://pyyaml.org/wiki/PyYAMLDocumentation:http://blog.csdn.net/conquer07 ...

  8. mac OS 查看开机/关机/重启记录

    last 查看最近的开关机.登录用户等记录 以及操作时间节点. last | grep reboot 查看重启记录 last | grep shutdown 查看关机记录

  9. HTML入门(HB、DW)

    一.文字内容 <b></b>  <strong></strong>     /*加粗 <i></i>   <em>& ...

  10. 墨仓式进入2.0时代?爱普生商用墨仓式L4158试用

    提起"墨仓式"打印机,相信现在已经没有人需要过多的解释,墨仓式打印机在打印市场占有率不断提高就是最佳佐证.为什么用户对于墨仓式这么认可,想必是墨仓式真正洞悉了他们的需求,解决了打印 ...