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 Specification:

Each input file contains one test case. Each case contains a pair of integers a and b where −. The numbers are separated by a space.

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

题目大意:给定两个数,计算两个数的和并且按照xxx,xxx,xxx这样的格式输出。
解题思路:
(1)计算两数之和。
(2)和是负数就输出一个符号并将和取绝对值,然后将和按位存入int类型数组中。
(3)从高位到低位输出数组中的数据,每隔三位输出一个逗号。
需要注意的点是:和为0的情况不能忽略,还有就是输出注意最后一位不输出逗号。
#include<iostream>

using namespace std;
int main() { int a, b;
cin >> a >> b;
int sum = a + b;
if (sum < 0) {
printf("-");
sum = -sum;
}
int res[7];
int len = 0;//记录长度
//和为0的情况
if (sum == 0) res[len++] = 0;
while (sum) {
//将sum按位存储,从低位到高位
res[len++] = sum % 10;
sum = sum / 10;
}
//输出时从高位到低位输出
for (int i = len - 1; i >= 0; i--) {
printf("%d", res[i]);
//每三位输出一个逗号,最后一位除外
if (i > 0 && i % 3 == 0) printf(",");
}
cout << endl;
system("pause");
return 0;
}
 

PAT A1001 A+B Format的更多相关文章

  1. PAT A1001 A+B Format (20 分)

    AC代码 #include <cstdio> #include <algorithm> using namespace std; const int maxn = 11; in ...

  2. pat 1001 A+B Format

    题目链接:传送门 题目简述: 1. 给定两个整数值a,b: 2.范围-1000000 <= a, b <= 1000000: 3.按指定格式输出结果 例:-100000 9 输出: -99 ...

  3. A1001. A+B Format

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

  4. PAT 1001. A+B Format 解题

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

  5. PAT 1001 A+B Format (20分) to_string()

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

  6. 【PAT】A1001A+B Format

    最新想法: 最多是七位数,而且只有一组输入,完全不用考虑算法复杂度. 直接判断是否为负,并输出符号 巧妙的地方:while循环的下一次再添加逗号.(防止出现,999,991的情况) 婼姐的方法真的很巧 ...

  7. PAT 1001. A+B Format(水题)

    #include<cstdio> #include<cstring> using namespace std; char s[10]; int main() { int a,b ...

  8. PAT 1001 A+B Format (20 point(s))

    题目: 我一开始的思路是: 用math.h中的log10函数来计算位数(不建议这么做,因为会很慢,而且会出一点别的问题): 用pow函数根据要插入分号的位置来拆分a+b成一个个数字(例如res / p ...

  9. PAT题目AC汇总(待补全)

    题目AC汇总 甲级AC PAT A1001 A+B Format (20 分) PAT A1002 A+B for Polynomials(25) PAT A1005 Spell It Right ( ...

随机推荐

  1. 泛型的类型擦除后,fastjson反序列化时如何还原?

    原创:微信公众号 码农参上,欢迎分享,转载请保留出处. 哈喽大家好啊,我是Hydra~ 在前面的文章中,我们讲过Java中泛型的类型擦除,不过有小伙伴在后台留言提出了一个问题,带有泛型的实体的反序列化 ...

  2. linux echo用法和实例

    echo命令用于在shell中打印shell变量的值,或者直接输出指定的字符串.linux的echo命令,在shell编程中极为常用, 在终端下打印变量value的时候也是常常用到的,因此有必要了解下 ...

  3. Ansible 使用配置

    1.配置 /etc/ansible/hosts 文件,添加被管控主机ip #vim /etc/ansible/hosts   文件末尾添加组[group1]和被管控主机的IP [group1] 192 ...

  4. 所有整数型包装类对象值的比较,使用equals方法进行比较

    一.整数型包装类对象值的比较,使用equals方法进行比较 题眼:整型包装类.值的比较 注:== :对于基本类型,比较的是值:对于引用类型,比较的是地址值. // 组一Integer i1=new I ...

  5. 如何将docker 镜像上传到docker hub仓库

    如何将docker 镜像上传到docker hub仓库 目录 如何将docker 镜像上传到docker hub仓库 背景 1.注册docker hub账号 2.docker hub上创建仓库 3.d ...

  6. Linux指令入门-系统管理(云小宝码上送祝福,免费抽iphone13任务)

    码上送祝福,带云小宝回家 做任务免费抽iphone13,还可得阿里云新春限量手办 日期:2021.12.27-2022.1.16 云小宝地址:https://developer.aliyun.com/ ...

  7. 4月2日 python学习总结

    昨天内容回顾: 1.迭代器 可迭代对象: 只要内置有__iter__方法的都是可迭代的对象 既有__iter__,又有__next__方法 调用__iter__方法==>得到内置的迭代器对象 调 ...

  8. Windows服务器上搭建Windows2003+IIS+ASP.NET+MSSQL网站

    一.安装IIS服务 1. 选择"开始"→"所有程序"→"管理工具"→"管理您的服务器"菜单命令,启动"添加或删 ...

  9. vue学习过程总结(07) - vue的后台服务API封装及跨域问题的解决

    以登录流程为例说明接口的封装. 1.登录调用后台的登录api 登录界面的代码 <template> <div class="login-page"> < ...

  10. JDK API文档_1.6.0 中文版

    链接:https://pan.baidu.com/s/1b0inUgYvEfjeusa3z_2p-g  密码:f8jk