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 ...
随机推荐
- python爬虫解决gbk乱码问题
今天尝试了下爬虫,爬取一本小说,忘语的凡人修仙仙界篇,当然这样不好,大家要支持正版. 爬取过程中是老套路,先获取网页源代码 # -*- coding:UTF-8 -*- from bs4 import ...
- Java基础学习笔记十五 集合、迭代器、泛型
Collection 集合,集合是java中提供的一种容器,可以用来存储多个数据. 在前面的学习中,我们知道数据多了,可以使用数组存放或者使用ArrayList集合进行存放数据.那么,集合和数组既然都 ...
- 201621123060《JAVA程序设计》第十四周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结与数据库相关内容. 2. 使用数据库技术改造你的系统 2.1 简述如何使用数据库技术改造你的系统.要建立什么表?截图你的表设计. 用 ...
- 库函数strstr的实现
没什么说的,常规思路: 函数原型:const char* StrStr(const char *str1, const char *str2) 方法一: str1:源字符串: str2:需要查找的目的 ...
- python3爬虫之入门和正则表达式
前面的python3入门系列基本上也对python入了门,从这章起就开始介绍下python的爬虫教程,拿出来给大家分享:爬虫说的简单,就是去抓取网路的数据进行分析处理:这章主要入门,了解几个爬虫的小测 ...
- MSSQL 2000 错误823恢复案例
一.故障描述 MSSQL Server 2000 附加数据库错误823,附加数据库失败.数据库没有备份,不能通过备份恢复数据库,急需恢复数据库中的数据. 二.故障分析SQL Server数据库 823 ...
- JAVA_SE基础——7.常量&变量
上一篇,我讲了标识符&关键字 这篇我来解释下变量&常量~~~ 变量与常量这两个概念相信大家都不会感到陌生,在数学中就已经涉及了变量与常量.理解变量与常量,可以举这样一个例子: 例 ...
- eclipse maven项目目录
今天遇见一个错误,关于eclipse项目的路径问题,web-inf的路径,上图和下图出现了两种web-INF,src的web-INFf和webContent的web-INF,src里面的文件需要编译以 ...
- io文本加密
文本加密习题 package io; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.Inpu ...
- 记一次向maven中央仓库提交依赖包
Maven是Java中最常用的依赖管理工具,Maven的中央仓库保罗万象,涵盖了各个领域的框架.工具和文档,也是Java生态强大生命力的体现.我们自己开发的一些有用有趣的代码也可以通过打包上传到mav ...