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. GitHub for VSCode

    GitHub for VSCode A first-party GitHub OAuth application (GitHub for VSCode) with repo and workflow ...

  2. web components & publish custom element & npm

    web components & publish custom element & npm https://www.webcomponents.org/publish Polymer ...

  3. UI & APP

    UI & APP lanhu http://help.lanhuapp.com/hc/ http://help.lanhuapp.com/hc/kb/article/1173434/ 快速使用 ...

  4. Flutter: provider 使用小部件的小部件构建的依赖注入系统

    文档 dependencies: provider: import 'package:dart_printf/dart_printf.dart'; import 'package:flutter/ma ...

  5. 在.NET中使用Apache Kafka(一)

    ​曾经在你的应用程序中使用过异步处理吗?在处理不需要立即执行的任务时,异步代码似乎是不可避免的.Apache Kafka是最常用和最健壮的开源事件流平台之一.许多公司和开发者利用它的强大功能来创建高性 ...

  6. 推荐一款好用的免费远程控制软件——ToDesk

    创作立场声明:我在本文中评测的软件为自用,感觉不错并且全免费,第一时间发出来和大家分享,欢迎理性观点交流碰撞. 疫情刚开始的时候,待在家里不能上班,但是还是有很多工作需要在线完成,常常需要跑回办公室拿 ...

  7. 总结 接口 final关键字 abstract关键字 static 关键字

    final关键字: * final 修饰的方法能被继承 不能被重写 * final修饰的类不能被继承 * final 修饰的变量(基本类型)不能被修改 * final 修饰的成员变量必须初始化 局部变 ...

  8. 边缘计算k8s集群之SuperEdge

    什么是边缘计算? 边缘计算,是指在靠近物或数据源头的一侧,采用网络.计算.存储.应用核心能力为一体的开放平台,就近提供最近端服务.其应用程序在边缘侧发起,产生更快的网络服务响应,满足行业在实时业务.应 ...

  9. DatePicker日期选择器的使用

    效果展示: 代码如下: <el-date-picker v-model="listQuery.times" type="daterange" range- ...

  10. HTTP 请求URL中不能含有空格

    如果含有空格 会报 不合法参数异常 正确做法是将其encode URLEncoder.encode(targetString, "utf-8").replaceAll(" ...