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. ShardingJdbc-分表;分库;分库分表;读写分离;一主多从+分表;一主多从+分库分表;公共表;数据脱敏;分布式事务

    目录 创建项目 分表 导包 表结构 Yml 分库 Yml Java 分库分表 数据库 Yml 读写分离 数据库 Yml 其他 只请求主库 读写分离判断逻辑代码 一主多从+分表 Yml 一主多从+分库分 ...

  2. linux作业--第四周

    1.自建yum仓库,分别为网络源和本地源 所有Yum仓库的配置文件均需以 .repo 结尾并存放在/etc/yum.repos.d/目录中的 [base] : yum仓库唯一标识符,避免与其它仓库冲突 ...

  3. 微信小程序搜索并高亮关键字

    更多解读可使用博客: https://www.jianshu.com/p/86d73745e01c 实现流程:1.在文本框中输入关键字key,如"比赛",检索出比赛相关的列表key ...

  4. PHP高并发商城秒杀

    1.什么是秒杀 秒杀活动是一些购物平台推出的集中人气的活动,一般商品数量很少,价格很便宜,限定开始购买的时间,会在以秒为单位的时间内被购买一空.比如原价千元甚至万元的商品以一元的价格出售,但数量只有一 ...

  5. 自己的markdown笔记

    markdown一些语法 记录自己会用的一些markdown语法,不定期更新,用的软件是hroopad,hroopad下载地址点击跳转.这个书写软件对新手还有中文用户比较友好,左边是markdown语 ...

  6. Jira8.0.2安装及破解

    最近开发部总监需要部署JIRA管理项目,就安装了一个JIRA8.5.7版安装并破解后,有天断电重启了,发现启动不了提示连接不上数据库.后来我又换了台机器重新安装后又进行了重启发现此破解版本存在问题.并 ...

  7. U8g2库的使用

    一.硬件介绍: 由于笔者这里只有0.96寸的OLED屏幕,那就讲讲最常用的0.96寸OLED屏幕吧. OLED介绍: OLED,即有机发光二极管( Organic Light Emitting Dio ...

  8. 原生 JS 实现 HTML 转 Markdown,以及其实现逻辑

    之前因为一些需要,需要转换部分 HTML 标签成 markdown 格式,但是不知不觉就完善到一个相对完整的函数. 然后我就封装成了一个文件放在了 github ,也简单做了两个示例网页. HTML ...

  9. ArcMap操作随记(11)

    1.直方图 在[Spatial Analyst]工具条中 2.分辨率变换 [重采样] :①最近邻法 ②双线性 ③三次卷积 重采样过程中要注意Nodata值 3.用ArcGIS进行监督分类 [影像分类] ...

  10. Django1.11 添加markdown语法支持

    pip install markdown 在view.py 的视图界面:导入,圈起来的那两个包 对post进行处理, models.py 详情如下  测试,效果如图