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. React Native 真机调试(iOS / Android)

    React Native 真机调试(iOS / Android) https://reactnative.dev/docs/running-on-device https://developer.ap ...

  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. 06_MySQL数据类型

    MySQL数据类型

  4. cxf实例异常

    基于CXF2.3.0 Caused by: java.lang.InstantiationException: org.apache.cxf.wstx_msv_validation.WoodstoxV ...

  5. Go的包

    目录 go的包 一.包的创建规则 二.包的导入规则 三.包的函数调用 go的包 一.包的创建规则 一个包就是一个文件夹. 同一个包(文件夹)下,所有go文件都只能用同一个package,也就是每个文件 ...

  6. AJAX基本操作

    XMLHttpRequest对象: XMLHttpRequest 是 AJAX 的基础.所有现代浏览器均支持 XMLHttpRequest 对象(IE5 和 IE6 使用 ActiveXObject) ...

  7. 大家最常用的编程论坛是哪个呢,欢迎评论!!掘金16 juejin 简书41 jianshu 博客85 csdn137 csdn

    软件编程交流论坛 掘金  16 juejin 简书  41 jianshu 博客  85 cnblogs csdn  137 csdn stackoverflow 0 思否 github 大家最常用的 ...

  8. 记录自己第一次搭建本地fabric框架

    写在前,第一次搭建fabric框架,对于小白的我很是艰辛,参考了很多博主的博客才最终完成,在此记录一下搭建过程. 参考的网站 https://blog.csdn.net/smallone233/art ...

  9. 爬虫必知必会(7)_scrapy框架高级

    一.请求传参 实现深度爬取:爬取多个层级对应的页面数据 使用场景:爬取的数据没有在同一张页面中 在手动请求的时候传递item:yield scrapy.Request(url,callback,met ...

  10. python基础学习之文件的基础操作方法

    打开文件方法 open('xx') 注意,open后括号内加的是文件名,这里默认是当前文件的相对路径,如果不在当前文件层,需要绝对路径,默认打开方法是读取,即read,默认的解码器为当前系统的解码器w ...