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的更多相关文章

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

  2. PAT (Advanced Level) Practice 1019 General Palindromic Number (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1019 General Palindromic Number (20 分) 凌宸1642 题目描述: A number that will ...

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

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

  5. PAT (Advanced Level) Practice 1001 A+B Format (20 分)

    题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805528788582400 Calculate a+b and ...

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

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

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

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

随机推荐

  1. API 授权 All In One

    API 授权 All In One 身份验证 授权类型 身份验证类型 继承认证 没有认证 API密钥 不记名令牌 基本认证 摘要授权 OAuth 1.0 OAuth 2.0 授权码 隐含的 密码凭证 ...

  2. Python Web Frameworks

    Python Web Frameworks top 10 Python web frameworks Django (Full-stack framework) Flask (Micro framew ...

  3. windows 设置右键菜单

    编辑注册表 在文件 右键菜单中添加 xx.reg: Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\*\shell\vscode] &q ...

  4. iPad pro & Mac mini

    iPad pro & Mac mini

  5. react-app 编写测试

    jest Enzyme 文档 为什么要写测试 单元测试(unit testing)指的是以软件的单元(unit)为单位,对软件进行测试.单元可以是一个函数,也可以是一个模块或组件.它的基本特征就是,只 ...

  6. Byte Buddy学习笔记

    本文转载自Byte Buddy学习笔记 简介 Byte Buddy是一个JVM的运行时代码生成器,你可以利用它创建任何类,且不像JDK动态代理那样强制实现一个接口.Byte Buddy还提供了简单的A ...

  7. 微服务学习.net5+consul

    趁着刚过完年,还没有开始做业务的时候,学习下consul 概念自己去官网看,这里只讲下具体实现 官网下载https://www.consul.io/downloads 我下载的是Windows版本 启 ...

  8. net字符串倒置和冒泡排序

    using System;using System.Configuration;using System.Data;using System.Linq;using System.Web;using S ...

  9. Linux-两种磁盘分区方式

    Linux文件设备 要理解Linux,首先要理解Linux文件结构 在Linux操作系统中,几乎所有的设备都位于/dev目录中 名称 作用 位置 SATA接口 电脑硬盘接口 /dev/sd[a-p] ...

  10. JAVA基础(一)—— 基础类型与面向对象

    JAVA基础(一)--基础类型与面向对象 1 数据类型 基本类型 byte short int long float double boolean char n 8 16 32 64 32 64 tr ...