1001 A+B Format (20)(20 分)

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

Input

Each input file contains one test case. Each case contains a pair of integers a and b where -1000000 <= a, b <= 1000000. The numbers are separated by a space.

Output

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

这里由于要在数字间加逗号,所以这里把数字和转化为字符串的形式(分离符号方便直接对数字操作,避免符号占1位导致后面位数判断出错)

注意:1、当数字>=0不需要+(=0:测试点4)

#include<iostream>
#include<string>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
string ch = a + b >= 0 ? "" : "-"; //分离符号
string sum = to_string(abs(a + b));
int k = 1;
for (int i = sum.length() - 1; i >= 0; k++, i--) { //每3位加",",除首位
if (k % 3 == 0 && i)
sum.insert(i, ",");
}
cout << ch + sum; //输出数字
return 0;
}

PAT 甲级1001 A+B Format (20)(C++ -思路)的更多相关文章

  1. PAT 甲级 1001 A+B Format (20)(20 分)

    1001 A+B Format (20)(20 分) Calculate a + b and output the sum in standard format -- that is, the dig ...

  2. PAT甲级 1001. A+B Format (20)

    题目原文: Calculate a + b and output the sum in standard format -- that is, the digits must be separated ...

  3. PAT甲级——1001 A+B Format (20分)

    Calculate a+b and output the sum in standard format – that is, the digits must be separated into gro ...

  4. PAT甲 1001. A+B Format (20) 2016-09-09 22:47 25人阅读 评论(0) 收藏

    1001. A+B Format (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Calculate ...

  5. 【PAT】1001. A+B Format (20)

    1001. A+B Format (20) Calculate a + b and output the sum in standard format -- that is, the digits m ...

  6. PAT甲级 1001 A+B Format

    题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805528788582400 1001 A+B Format ( ...

  7. PAT 甲级 1001 A+B Format

    https://pintia.cn/problem-sets/994805342720868352/problems/994805528788582400 Calculate a + b and ou ...

  8. PAT Advanced 1001 A+B Format (20 分)

    Calculate a+b and output the sum in standard format -- that is, the digits must be separated into gr ...

  9. 1001.A+B Format (20)(思路,bug发现及其修改,提交记录)

    https://github.com/031502316a/object-oriented/tree/master/1001 ---恢复内容开始--- 1.解题思路 一开始见到题目时,感觉难的就是输出 ...

随机推荐

  1. canvas入门笔记

    1.Canvas的使用注意  A.要在页面中添加一对canvas标记,默认占300*150的区域  B.我们可以通过html属性‘width’,‘height’来设置canvas的宽高,不可以通过cs ...

  2. unity 数学公式

    Mathf.Abs绝对值 计算并返回指定参数 f 绝对值. Mathf.Acos反余弦 static function Acos (f : float) : float 以弧度为单位计算并返回参数 f ...

  3. mab算法

    https://zhuanlan.zhihu.com/p/21388070?refer=resyschina 专治选择困难症——bandit算法 改善:https://zhuanlan.zhihu.c ...

  4. idea 设置 转自 https://www.cnblogs.com/jajian/p/8136672.html

    前面已经介绍过Settings上中部分,接下来继续剩余的部分 IntelliJ IDEA(四) :Settings(上) IntelliJ IDEA(五) :Settings(中) 0|1一.Buil ...

  5. Codeforces Round #499 (Div. 2) C Fly题解

    题目 http://codeforces.com/contest/1011/problem/C Natasha is going to fly on a rocket to Mars and retu ...

  6. 1.3.8、CDH 搭建Hadoop在安装之前(端口---Apache Flume和Apache Solr使用的端口)

    Apache Flume和Apache Solr使用的端口 Apache Flume用于与Apache Solr通信的端口可能会有所不同,具体取决于您的配置以及是否使用安全性(例如,SSL).使用Fl ...

  7. SVN Commit:将本地代码更新到服务器代码

    1.点击客户端“TortoiseSVN” 选中后显示: 点击Import: 点击“ok”:

  8. tomcat实现https

    第一步:生成key文件: C:\>keytool -genkey -alias tomcat -keyalg RSA -keystore C:\tomcat.key 密码最好设置默认change ...

  9. ISE软件报错

    ISE弹出如下报错并关闭程序或在编译时出现PATH类报错 一,解决办法 本人自己试了一下  E:\ISE\14.7\ISE_DS\settings64.bat E:\ISE\14.7\ISE_DS\I ...

  10. 更新linux下python版本

    # 安装所有的开发工具包 yum groupinstall -y "Development tools" # 安装其它的必需包 yum install -y zlib-devel ...