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 ...
随机推荐
- react: nextJs koa project basic structure
1.init nextJs project npm init npm install react react-dom next config script in package.json " ...
- Uva 1754 Posterize
#include<bits/stdc++.h> using namespace std; #define rep(i,a,b) for(int i=a;i<=b;++i) #defi ...
- 【Linux网络基础】网络拓扑、OSI层次模型、TCP/IP协议簇
一.前言 一个运维有时也要和网络打交道,所以具备最基本的网络知识,对一个运维人员来说是必要的.但,对于我们的工作来说这些并不是重点,因此,我不可能从最基础的网络知识开始讲起.本节内容更多是从一个梳理和 ...
- java 设计模式-责任链
责任链设计模式,其实就是处理同一个请求的对象连接成一条链,请求的路径经过这条链,符合要求的就处理这个请求,不符合就接着往下面抛出,直道有人处理这条请求. 业务:比如啊,公司个人请假,三天以下就是主管审 ...
- JavaScript实现选择排序
一.选择排序简介 **冒泡排序.插入排序.选择排序合称为简单排序.**下面是选择排序的思想: 假设有一个数组a,我们想象成有一个班级名叫a班,现在全班随意排成一排,排头的位置是a[0],排尾的位置是a ...
- 2019/02/16 STL容器 :栈
一.栈(stack) 1.定义: 栈是一种只能在某一端插入和删除数据的特殊线性表.他按照先进先出的原则存储数据,先进的数据被压入栈底,最后进入的数据在栈顶,需要读数据的时候从栈顶开始弹出数据(最后被压 ...
- Windows 10配置VS Code C++环境(超详细,面向小白以及大佬们)
看完这个,还有下一篇:门在这 我看了网上的大佬们配的我是在是看不懂啊?我是一个小白啊?这太难了,这阻挡不了我,想使用这很骚的IDE,于是在不断的摸索下,终于配置成功,小白们也不用慌,这次非常简单.一定 ...
- 解决python语言的工具pycharm以及Windows电脑安装pygame模块的问题
人生苦短,我用python,python作为一门当今时代潮流性的语言,已经成为大多数的年轻程序猿们向往的目标,python中有许多的库, 其中有一个pygame库是作为开发2D游戏必不可少的开发库,是 ...
- nginx代理vue项目
很多项目的前端都使用vue编写的,在项目上线部署的时候,有些项目要求把前端页面和后台服务部署在不同的服务器,这就要求使用nginx代理,本文就来讲讲vue项目怎么使用nginx代理. 项目github ...
- webpack-基础知识
一.webpack介绍 webpack是一个前端模块化工具,简单解释:webpack就是处理多个文件,根据设置的规则,对文件进行合并和修改. 正式说:webpack是一个模块化打包工具.从入口模块出发 ...