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 in standard format -- that is, the digits must be separated into groups of three by commas (unless there are less than four digits).
译:计算 a + b 的和,并格式化输出——即,数字必须每三个作为一组,用逗号隔开(除非少一四位数字)
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.
译:每个输入文件包含一个测试用例,每个测试用例包含一个整数 a 和一个整数 b , 10-6 ≤ a , b ≤ 10 6 数字之间用空格分开。
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
The Idea:
/*
根据 a 和 b 的范围知,int 完全够用 先计算 c = a + b 存储 a + b 的和,
然后根据其的正负确定是否需要输出负号 “ - ”
然后将 c 转成 string 类型,利用循环遍历一次,当从末尾且从1计数开始,如果这个位置是3的倍数
意味着需要加一个逗号(string 的长度刚好是3的倍数时第一个除外) ;
遍历完成之后,直接输出所得字符串即可
*/
The Codes:
#include<bits/stdc++.h>
using namespace std ;
int main(){
int a , b , c ;
cin >> a >> b ;
c = a + b ;
string s , ans = "" ;
if(c < 0) ans += "-" ;
s = to_string(abs(c)) ;
for(int i = 0 ; i < s.size() ; i ++){
if((s.size() - i) % 3 == 0 && i != 0) ans += "," ; // 加逗号的情况
ans += s[i] ;
}
cout<< ans << endl ;
}
PAT (Advanced Level) Practice 1001 A+B Format (20 分) 凌宸1642的更多相关文章
- PAT (Advanced Level) Practice 1027 Colors in Mars (20 分) 凌宸1642
PAT (Advanced Level) Practice 1027 Colors in Mars (20 分) 凌宸1642 题目描述: People in Mars represent the c ...
- PAT (Advanced Level) Practice 1019 General Palindromic Number (20 分) 凌宸1642
PAT (Advanced Level) Practice 1019 General Palindromic Number (20 分) 凌宸1642 题目描述: A number that will ...
- PAT (Advanced Level) Practice 1011 World Cup Betting (20 分) 凌宸1642
PAT (Advanced Level) Practice 1011 World Cup Betting (20 分) 凌宸1642 题目描述: With the 2010 FIFA World Cu ...
- PAT (Advanced Level) Practice 1005 Spell It Right (20 分) 凌宸1642
PAT (Advanced Level) Practice 1005 Spell It Right (20 分) 凌宸1642 题目描述: Given a non-negative integer N ...
- PAT (Advanced Level) Practice 1001 A+B Format (20 分)
题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805528788582400 Calculate a+b and ...
- PAT (Advanced Level) Practice 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 (Advanced Level) Practice 1027 Colors in Mars (20 分)
People in Mars represent the colors in their computers in a similar way as the Earth people. That is ...
- PAT (Advanced Level) Practice 1019 General Palindromic Number (20 分) (进制转换,回文数)
A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...
- PAT (Advanced Level) Practice 1054 The Dominant Color (20 分)
Behind the scenes in the computer's memory, color is always talked about as a series of 24 bits of i ...
随机推荐
- git config [section] solutions
git config [section] solutions fix git [section] warnings global config $ vim ~/.gitconfig [user] em ...
- how to convert Map to Object in js
how to convert Map to Object in js Map to Object just using the ES6 ways Object.fromEntries const lo ...
- 如何使用 iMovie 去除视频里面的声音
如何使用 iMovie 去除视频里面的声音 视频去除背景音 iMovie https://www.apple.com/imovie/ https://books.apple.com/book/id14 ...
- queueMicrotask & microtask
queueMicrotask & microtask microtask microtask queue Promise Mutation Observer API MutationObser ...
- py django 渲染前端打包的视图
前端打包后基本这样 $ ls dist /static index.html 在index.html中的publicPath指向static 1. 创建一个www模块 $ python manage. ...
- [Python] Matplotlib 图表的绘制和美化技巧
目录 在一张画布中绘制多个图表 加图表元素 气泡图 组合图 直方图 雷达图 树状图 箱形图 玫瑰图 在一张画布中绘制多个图表 Matplotlib模块在绘制图表时,默认先建立一张画布,然后在画布中显示 ...
- Linux解压缩相关命令
Linux解压缩相关命令 运行级别: 0:关机 1:单用户 2:多用户无网络连接 3:多用户有网络连接 4:系统保留 5:图形界面 6:系统重启 通过init[0123456]来切换不同的运行级别 g ...
- Django的视图层和模板层
目录 一.视图层 1. 小白必会三板斧 2. JsonResponse 3. FBV与CBV 3.1 FVB 3.2 CBV 4. CBV的源码 5. 给CBV加装饰器 二.模板层 1. 模板语法 2 ...
- SpringCloud(四):服务注册中心Eureka Eureka高可用集群搭建 Eureka自我保护机制
第四章:服务注册中心 Eureka 4-1. Eureka 注册中心高可用集群概述在微服务架构的这种分布式系统中,我们要充分考虑各个微服务组件的高可用性 问题,不能有单点故障,由于注册中心 eurek ...
- windows上传ipa文件到苹果开发者中心的教程
转: windows上传ipa文件到苹果开发者中心的教程 我们在苹果开发者中心上架ios app的时候,需要使用xcode或transporter先上传ipa文件到开发者中心. 但是假如我们只是H5开 ...