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. Hadoop3.x完全分布式搭建(详细)

    环境准备 vm虚拟机(自行安装Centos7系统) hadoop3.x安装包(linux版本) java1.8安装包(linux版本) 为了能够按照教程顺利操作,需要注意几点细节 不要不看文字直接复制 ...

  2. C++雾中风景18:C++20, 从concept开始

    转眼间,C++20的标准已经发布快两年了.不少C++的开源项目也已经将标准升级到最新的C++20了,笔者也开启了新标准的学习历程了.所以借这系列的博文,记录下笔者学习新标准的一些心得与吐槽~~ 作为C ...

  3. layui 数据表格的使用(分页+总条数)

    下载地址 https://www.layui.com/ 点击实例,找到layui适合模板 2. 新建html将代码复制到对应模板,修改对应样式路径. 5.修改对应参数(url,field) 追加以下参 ...

  4. const 对象的属性能否修改

    const保证的并不是变量的值不能改动,而是变量指向的那个内存地址不能改动. 对于基本类型的数据(数值.字符串.布尔值),其值就保存在变量指向的那个内存地址,因此等同于常量. 对于引用类型的数据(主要 ...

  5. ASP.NET Core 6框架揭秘实例演示[22]:如何承载你的后台服务[补充]

    借助 .NET提供的服务承载(Hosting)系统,我们可以将一个或者多个长时间运行的后台服务寄宿或者承载我们创建的应用中.任何需要在后台长时间运行的操作都可以定义成标准化的服务并利用该系统来承载,A ...

  6. docker学习笔记(3)- 镜像

    简介 在docker学习笔记(1)- 架构概述一节中可以看到镜像是docker三大组件之一,可以将Docker镜像类比为虚拟机的模版. 镜像由多个层组成,每层叠加之后从外部看就像一个独立的对象,镜像的 ...

  7. 聊聊磁盘 IO

    常见的磁盘类型 按存储原理的不同,可以把磁盘分为这么几种 HDD 盘:没啥说的,就是平时最常见的机械盘. SSD 盘:用电信号来记录存储数据,而不是磁片.显然进行 I/O 时,这要比机械盘的物理寻址方 ...

  8. TCP | 你真的懂 HTTP 吗?

    前言 Hello 大家好,我是 Sam Zhang. HTTP 相信是每个 Web 开发者都耳熟能详的名词了.但是,新手开发者想要完全理解 HTTP 协议却需要时间.这期视频,我就来带大家入门 HTT ...

  9. 4月19日 python学习总结 套接字模块的使用

    服务端: import socket phone=socket.socket(socket.AF_INET,socket.SOCK_STREAM) # 买电话 phone.bind(('127.0.0 ...

  10. VULNCMS

    靶机准备 导入虚拟机,并将网络模式设置为NAT 扫描ip netdiscover -r 192.168.164.0/24 渗透测试 扫描端口 nmap -sS -sV -T5 -A -p- 192.1 ...