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. WPF界面语言切换

    举例中英文切换: 一.静态切换(每次切换需要重启应用) 1. 新建一个WPF APP(.NET Framework)项目,StaticLanguageSelect 2. 右击项目名,添加New Ite ...

  2. 扩展auth_user字段、BBS需求分析、创建BBS数据库、注册页面搭建与用户头像展示及Ajax提交数据

    昨日内容回顾 csrf跨站请求 1. SQL注入 2. xss攻击 3. csrf跨站请求 4. 密码加密(加盐) '''django中默认有一个中间件来验证csrf''' # 只针对post请求才验 ...

  3. lavarel 框架 搜索后分页

    ................框架控制器 public function list(Request $request){ $word=$request->input('word'); $arr ...

  4. 2.6 C++STL queue详解

    文章目录 2.6.1 引入 2.6.2 代码示例 2.6.3 代码运行结果 总结 2.6.1 引入 首先,在STL中 queue 和 stack 其实并不叫容器(container),而是叫适配器(a ...

  5. 使用vscode Container开发调试envoy

    由于我最近在研究 envoy 这个项目,这是个cpp的项目,对于我这种cpp新人来说还是比较有压力的,感觉处处都是坑,开个引导文章记录一下. 如果要研究 envoy 项目源码,那肯定是需要代码跳转的, ...

  6. Java实例变量、局部变量、静态变量

    实例变量(成员变量) 成员变量定义在类中,在整个类中都可以被访问,但在方法.构造方法和语句块之外 当一个对象被实例化之后,每个实例变量的值就跟着确定 实例变量在对象创建的时候创建,在对象被销毁时销毁 ...

  7. Apple macOS Mojave Intel Graphics Driver组件任意代码执行漏洞

    受影响系统:Apple macOS Mojave 10.14.5描述:CVE(CAN) ID: CVE-2019-8629 Apple macOS Mojave是苹果公司Mac电脑系列产品的操作系统. ...

  8. 5月11日 python学习总结 子查询、pymysql模块增删改查、防止sql注入问题

    一.子查询 子查询:把一个查询语句用括号括起来,当做另外一条查询语句的条件去用,称为子查询 select emp.name from emp inner join dep on emp.dep_id ...

  9. python域名200检测

    import requests import threading import queue # qianxiao996精心制作 #博客地址:https://blog.csdn.net/qq_36374 ...

  10. k8s集群Job Pod 容器可能因为多种原因失效,想要更加稳定的使用Job负载,有哪些需要注意的地方?

    k8s集群Job Pod 容器可能因为多种原因失效,想要更加稳定的使用Job负载,有哪些需要注意的地方? 面试官:"计数性Job默认完成模式是什么?Indexed模式如何发布自定义索引呢?& ...