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 ...
随机推荐
- 两种设计模式和XML解析
两种设计模式 1.单例模式 模式的保证步骤:单例(是说在一个类中只能有一个对象)三条件 1.1类构造设置私有 private Play() { } 1.2 定义一个私有的静态的 类类型 变量 ...
- 『转载』从内存资源中加载C++程序集:CMemLoadDll
MemLoadDll.h #if !defined(Q_OS_LINUX) #pragma once typedef BOOL (__stdcall *ProcDllMain)(HINSTANCE, ...
- JavaScript(第二天)【语法,变量】
一.语法构成 区分大小写 ECMAScript中的一切,包括变量.函数名和操作符都是区分大小写的.例如:text和Text表示两种不同的变量. 标识符 所谓标识符,就是指变量.函数.属性的名字,或 ...
- 第七周PTA作业
第一题: #include<stdio.h> int main() { ; ; ){ sum=sum+i; i++; } printf("sum = %d\n",sum ...
- 敏捷冲刺每日报告二(Java-Team)
第二天报告(10.26 周四) 团队:Java-Team 成员: 章辉宇(284) 吴政楠(286) 陈阳(PM:288) 韩华颂(142) 胡志权(143) github地址:https://gi ...
- networkx 学习
import networkx as nx import pylab import numpy as np #自定义网络 row=np.array([,,,,,,]) col=np.array([,, ...
- 201621123057 《Java程序设计》第8周学习总结
1. 本周学习总结 思维导图归纳总结集合相关内容. 2. 书面作业 1. ArrayList代码分析 1.1 解释ArrayList的contains源代码 ArrayList是允许重复的,但当用它来 ...
- Flask 学习 十五 性能
记录影响性能的数据库查询 app/main/views.py from flask_sqlalchemy import get_debug_queries @main.after_app_reques ...
- php函数var_dump() 、print_r()、echo()
var_dump() 能打印出类型 print_r() 只能打出值 echo() 是正常输出... 需要精确调试的时候用 var_dump(); 一般查看的时候用 print_r() 另外 , ech ...
- thinkphp调试技巧
调试的经验:很多时候程序调试不出来,但是又找不出错误,往往是拼写错误可能是很小的拼写错误,很难看出,或者多了一个空格,比如在配置路由的时候'URL_ROUTER_ON '=true,这样设置就会错误, ...