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 ...
随机推荐
- 取代 Python 多进程!伯克利开源分布式框架 Ray
Ray 由伯克利开源,是一个用于并行计算和分布式 Python 开发的开源项目.本文将介绍如何使用 Ray 轻松构建可从笔记本电脑扩展到大型集群的应用程序. 并行和分布式计算是现代应用程序的主要内容. ...
- MySQL中出现Unknow column 'xx' in field list的解决办法
首先创建一个表,然后插入数据发现出错误 经过多次尝试发现title前面多了一个空格 我们把空格去掉,然后在插入数据,发现数据创建成功
- java传参问题
参考链接:https://www.cnblogs.com/linkstar/p/5951141.html public class Example { String testString = publ ...
- 0day堆(2)堆的调试实验
堆的调试实验 调试态堆管理策略和常态堆管理策略:前者只使用空表不用块表,不真实 使用调试器加载函数会触发前者 __asm int3 调试最真实的栈 未启用块表的堆区信息 堆区起始位置(假设为0x005 ...
- JavaScript学习笔记(1)字符串方法
字符串方法 length 属性返回字符串的长度 var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; var sln = txt.length; inde ...
- php---算法和数据结构
<?php header("content-type:text/html;charset=utf-8"); $arr = array(3,5,8,4,9,6,1,7,2); ...
- 我们常听到的WAL到底是什么
什么是 WAL WAL(Write Ahead Log)预写日志,是数据库系统中常见的一种手段,用于保证数据操作的原子性和持久性. 在计算机科学中,预写式日志(Write-ahead logging, ...
- python学习12类
'''''''''类:具有相同特性和行为的对象抽象为类特性——>属性Property行为——>方法class:关键字'''class Boxes():#类的第一行格式 '''立方体类''' ...
- 2019-2020-1 20199328《Linux内核原理与分析》第三周作业
加载内核 这里可以看出有些东西隔一段时间就会打印出来 查看mymain.c 开头的一些语句不再描述 每10000次循环打印一次 这里还是针对的mymain.c文件,这里我们可以根据自己的计算机对频率进 ...
- 利用requests, beautifulsoup包爬取股票信息网站
这是第一次用requests, beautifulsoup实现爬虫,此次爬取的是一个股票信息网站:http://www.gupiaozhishi.net.cn. 实现非常简单,只是为了demo使用的数 ...