pat 1001 A+B Format
题目链接:传送门
题目简述:
1. 给定两个整数值a,b;
2.范围-1000000 <= a, b <= 1000000;
3.按指定格式输出结果
例:-100000 9
输出: -99,991
解题思路:
1.明确范围
a+b在正负两百万范围内, 32位系统int类型占4字节精度够
2.明确要求:
① 输入以空格分割, 输入整数
②结果如果数字大于4位, 需要每三位用逗号分割
③视算法可能有需要补零的情况(我就是踩的这个坑)
④正负号提前判定, 便于后面处理
⑤函数要以return 0; 结束(这是我提交代码后发现的)
3.采取措施:
①将相加的结果循环对1000取余, 余数存在数组里
②输出数组中数字的最高位(最高位不存在需要补零的情况)
③ 用printf("%03d"), 实现补零。
4.潜在问题:
①视代码的具体实现方式可能在处理0的时候会出问题;
②对应措施:打完代码特别观察一下0的情况,并手测数据即可
5、提交后仍存在的bug
无;
源代码:
#include<stdio.h>
int main()
{ int a=0, b=0, sum=0;
int format[10] = {0};
int i = 0;
scanf("%d %d", &a, &b);
sum = a+b;
if (sum < 0) {
printf("-");
sum = -sum;
} while((sum/1000) >0) {
format[i] = sum%1000;
sum = sum/1000;
i++;
} format[i] = sum;
for (printf("%d", format[i]), i--; i>=0;i--) {
printf(",%03d", format[i]); }
return 0;
}
结果截图:

本文撰文格式和部分代码参考:http://www.cnblogs.com/andwho/p/5161998.html
十分感谢!
pat 1001 A+B Format的更多相关文章
- 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 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 (Advanced Level) Practice 1001 A+B Format (20 分) 凌宸1642
PAT (Advanced Level) Practice 1001 A+B Format (20 分) 凌宸1642 题目描述: Calculate a+b and output the sum i ...
- PAT甲级 1001 A+B Format
题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805528788582400 1001 A+B Format ( ...
- PAT 甲级 1001 A+B Format (20)(20 分)
1001 A+B Format (20)(20 分) Calculate a + b and output the sum in standard format -- that is, the dig ...
- PAT 甲级1001 A+B Format (20)(C++ -思路)
1001 A+B Format (20)(20 分) Calculate a + b and output the sum in standard format -- that is, the dig ...
- PAT甲 1001. A+B Format (20) 2016-09-09 22:47 25人阅读 评论(0) 收藏
1001. A+B Format (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Calculate ...
随机推荐
- 真是没想到,ikvm.net居然停止开发了。
看样子作者对.net已经失去了信心 http://weblog.ikvm.net/CommentView.aspx?guid=33ea525f-a291-418a-bd6a-abdf22d0662b# ...
- c# 实时监控数据库 SqlDependency
http://blog.csdn.net/idays021/article/details/49661855 class Program { private static string _connSt ...
- 十个 PHP 开发者最容易犯的错误
PHP 语言让 WEB 端程序设计变得简单,这也是它能流行起来的原因.但也是因为它的简单,PHP 也慢慢发展成一个相对复杂的语言,层出不穷的框架,各种语言特性和版本差异都时常让搞的我们头大,不得不浪费 ...
- 巨人大哥谈Java中的Synchronized关键字用法
巨人大哥谈Java中的Synchronized关键字用法 认识synchronized 对于写多线程程序的人来说,经常碰到的就是并发问题,对于容易出现并发问题的地方价格synchronized基本上就 ...
- Java8学习(4)-Stream流
Stream和Collection的区别是什么 流和集合的区别是什么? 粗略地说, 集合和流之间的差异就在于什么时候进行计算.集合是一个内存中的数据结构,它包含数据结构中目前所有的值--集合中的每个元 ...
- electron-vue工程创建
没有vue创建经验请移步至 vue下载与安装 使用vue创建electron-vue工程 vue init simulatedgreg/electron-vue my-project 安装elemen ...
- DML数据操作语言之复杂查询
1.视图(View) 我们知道,在关系型数据库中,用来保存实际数据记录的是数据表.和表同等概念也是用来保存东西是:视图. 但是数据表是用来保存实际数据记录的,而视图是用来保存常用select语句的. ...
- 读论文系列:Object Detection ECCV2016 SSD
转载请注明作者:梦里茶 Single Shot MultiBox Detector Introduction 一句话概括:SSD就是关于类别的多尺度RPN网络 基本思路: 基础网络后接多层featur ...
- Hazelcast分布式
一般的应用正式环境中都不止一台服务器(也就是说是集群的),那么如果只是简单的将数据预加载到内存,那么就会有数据不同步的现象. (更新了其中一台JVM,另一台JVM并不会收到通知从而保持数据同步). 这 ...
- 用javascript做别踩白块游戏1
初学Javascript做的一个别踩白块小游戏,代码简陋,如下: <!DOCTYPE html> <html> <head> <!-- 禁用缩放功能 --&g ...