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 ...
随机推荐
- oracle管理权限和角色
介绍 这一部分主要看看oracle中如何管理权限和角色,权限和角色的区别在哪里. 当刚刚建立用户时,用户没有任何权限,也不能执行任何操作.如果要执行某种特定的数据库操作,则必需为其授予系统的权限:如果 ...
- 一个基于H5audio标签的vue音乐播放器
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Comparable接口和Comparator接口
1.一个类在设计之初就要实现对该类对象的排序功能,那么这个类要实现Comparable接口,实现public int compareTo(T t)方法.如代码中的Student类.对于实现Compar ...
- JavaScript(第四天)【运算符】
ECMA-262描述了一组用于操作数据值的运算符,包括一元运算符.布尔运算符.算术运算符.关系运算符.三元运算符.位运算符及赋值运算符.ECMAScript中的运算符适用于很多值,包括字符串.数值.布 ...
- C语言——第六周作业
题目 题目一:高速公路超速处罚 1.实验代码 #include <stdio.h> int main() { int speed,maxspeed; double x; scanf(&qu ...
- iOS开发之Objective-C与JavaScript的交互
UIWebView是iOS最常用的SDK之一,它有一个stringByEvaluatingJavaScriptFromString方法可以将javascript嵌入页面中,通过这个方法我们可以在iOS ...
- zookeeper入门系列:paxos协议
上一章讨论了一种强一致性的情况,即需要分布式事务来解决,本章我们来讨论一种最终一致的算法,paxos算法. paxos算法是由大牛lamport发明的,关于paxos算法有很多趣事.比如lamport ...
- thinkphp中的常见静态常亮
thinkphp __PUBLIC__的定义 __ROOT__等常量的定义 1 2 3 4 5 6 7 8 9 '__TMPL__' => APP_TMPL_PATH, // 项目 ...
- 阿里云API网关(7)开发指南-API参考
网关指南: https://help.aliyun.com/document_detail/29487.html?spm=5176.doc48835.6.550.23Oqbl 网关控制台: https ...
- Python学习之list有序集合
# coding=utf-8 # list有序集合 classmate = ['Michael', 'Bob', 'Tracy'] print classmate print len(classmat ...