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. DOM & Node.contains

    DOM & Node.contains Node.contains() https://developer.mozilla.org/en-US/docs/Web/API/Node/contai ...

  2. how to recursively all files in a folder with sudo permissions in macOS

    how to recursively all files in a folder with sudo permissions in macOS write bug OK sudo chmod 777 ...

  3. NGK八大板块:为何郊区市场近来火爆?-VALAITIS, PETER ANTHONY分析

    PAUL ADAMS ARCHITECT LTD董事长VALAITIS, PETER ANTHONY称受大环境影响很多纽约人都选择离开纽约市中心,搬往附近郊区,因此附近地区楼市开始不断升温. 根据房地 ...

  4. NGK.IO的智能合约是炒作还是未来商业的主流?

    随着NGK主网的上线,NGK市场也备受关注.目前,NGK代币价格已经由初始价格0.0215美元涨到现在的0.86美元,代币价格上涨40倍!数字货币市场也已经将重点目光放到了NGK代币上,相信在不久的将 ...

  5. 类属性和__init__的实例属性有何区别?进来了解一下吧

    真的是随笔写的一篇,以防日后记忆模糊,特此记录.大佬勿喷 疑问:类属性和实例属性有何区别? 正题,代码如下 age为People类的属性(称为类属性) name是在__init__方法下,在创建实例对 ...

  6. 高性能环形队列框架 Disruptor 核心概念

    高性能环形队列框架 Disruptor Disruptor 是英国外汇交易公司LMAX开发的一款高吞吐低延迟内存队列框架,其充分考虑了底层CPU等运行模式来进行数据结构设计 (mechanical s ...

  7. 1086 Tree Traversals Again——PAT甲级真题

    1086 Tree Traversals Again An inorder binary tree traversal can be implemented in a non-recursive wa ...

  8. 如何用Eggjs从零开始开发一个项目(1)

    前言 "纸上得来终觉浅,绝知此事要躬行."虽然node一直在断断续续地学,但总是东一榔头西一榔头的,没有一点系统,所以打算写一个项目来串联一下之前的学习成果. 为什么选择Eggjs ...

  9. selenium之元素定位的方法(一)

    WebDriver 对象有多种方法用于在页面中寻找元素.他们被分成find_element_*和find_elements_*方法.find_element_*方法返回一个WebElement对象,代 ...

  10. TransactionScope 事务

    一.TransactionScope是.Net Framework 2.0之后,新增了一个名称空间.它的用途是为数据库访问提供了一个"轻量级"[区别于:SqlTransaction ...