PAT A1001 A+B Format
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的更多相关文章
- PAT A1001 A+B Format (20 分)
AC代码 #include <cstdio> #include <algorithm> using namespace std; const int maxn = 11; in ...
- pat 1001 A+B Format
题目链接:传送门 题目简述: 1. 给定两个整数值a,b: 2.范围-1000000 <= a, b <= 1000000: 3.按指定格式输出结果 例:-100000 9 输出: -99 ...
- A1001. A+B Format
Calculate a + b and output the sum in standard format -- that is, the digits must be separated into ...
- 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 ...
- 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 ...
- 【PAT】A1001A+B Format
最新想法: 最多是七位数,而且只有一组输入,完全不用考虑算法复杂度. 直接判断是否为负,并输出符号 巧妙的地方:while循环的下一次再添加逗号.(防止出现,999,991的情况) 婼姐的方法真的很巧 ...
- PAT 1001. A+B Format(水题)
#include<cstdio> #include<cstring> using namespace std; char s[10]; int main() { int a,b ...
- PAT 1001 A+B Format (20 point(s))
题目: 我一开始的思路是: 用math.h中的log10函数来计算位数(不建议这么做,因为会很慢,而且会出一点别的问题): 用pow函数根据要插入分号的位置来拆分a+b成一个个数字(例如res / p ...
- PAT题目AC汇总(待补全)
题目AC汇总 甲级AC PAT A1001 A+B Format (20 分) PAT A1002 A+B for Polynomials(25) PAT A1005 Spell It Right ( ...
随机推荐
- HDFS连接JAVA,HDFS常用API
先在pom.xml中导入依赖包 <dependencies> <!-- https://mvnrepository.com/artifact/org.apache.hadoop/ha ...
- SpringBoot连接Redis (Sentinel模式&Cluster模式)
一.引入pom <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
- 微信小程序授权获取手机号
wxml: <text>pages/logins/logins.wxml</text> // <button open-type="getPhoneNumber ...
- docker学习(一) - docker简介
(一)Docker是什么? Docker 是一个开源的应用容器引擎,你可以将其理解为一个轻量级的虚拟机,开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上 ...
- S2-045(RCE远程代码执行)
环境搭建: https://blog.csdn.net/qq_36374896/article/details/84145020 漏洞复现 进入漏洞环境 (048和045一样) cd vulhub-m ...
- Map的野路子
首先有一张user数据表,数据库名称为mybatis,数据如下: 我们使用以下两种方式实现数据更新的操作. 方式一 UserMapper.java如下: /** * @description: 更改用 ...
- KestrelServer详解[2]: 网络链接的创建
<注册监听终结点(Endpoint)>已经详细讲述了如何使用KestrelServer,现在我们来简单聊聊这种处理器的总体设计和实现原理.当KestrelServer启动的时候,注册的每个 ...
- 深度优先算法--对DFS的一些小小的总结(一)
提到DFS,我们首先想到的是对树的DFS,例如下面的例子:求二叉树的深度 int TreeDepth(BinaryTreeNode* root){ if(root==nullptr)return 0; ...
- kafka端口和zookeeper端口
一.问题描述 今天配合现场联调一个数据工具,工具使用到了kafka,程序启动之后包如下错误: [WARN ] [2020-08-17 19:17:27] [org.apache.kafka.clien ...
- Mybaits 的优点?
1.基于 SQL 语句编程,相当灵活,不会对应用程序或者数据库的现有设计造成任 何影响,SQL 写在 XML 里,解除 sql 与程序代码的耦合,便于统一管理:提供 XML 标签,支持编写动态 SQL ...