PAT----1001. A+B Format (20)解题过程
1001. A+B Format (20)
github链接

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
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
Each input file contains one test case. Each case contains a pair of integers a and b where -1000000 <= a, b <= 1000000. The numbers are separated by a space.
Output
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
题目大意
输入整数 a , b(-1000000 <= a, b <= 1000000);计算a,b的和;
重点是:结果要按照“三位分级法”输出。(e.g -999,991 )
解题思路
第一次读题后首先想到的是数组和字符串,但是实现起来有难度(如果用c++可能会好一些,但是我还没学完!),而且就算能用C语言实现,也很繁琐。所以最后还是决定用一些最基础的算法去解决这道题。
- 这道题主要考的是 三位分级法,而且-2000000<= sum <= 2000000;所以我们可以分类讨论:(设 c 为sum的绝对值,因为结果有负数,所以要用绝对值进行分类讨论)
- C<1000 (直接输出sum)
- 1000<= C <1000000 (有一个逗号)
- C>=1000000 (有两个逗号)
最后,每种情况对应一种输出方式;然后输出结果。


我对照着编译运行的结果又重新检查了一遍,发现代码中输出格式出错,逗号之后的数位要用0补齐;并且,中间三位数的算法出错!
在修改完代码之后,成功 AC!




pdf版本
PAT----1001. A+B Format (20)解题过程的更多相关文章
- "1001. A+B Format (20)" 解题报告
Github : git@github.com:Circlecos/object-oriented.git PDF Of Markdown : "1001. A+B Format (20)& ...
- 1001.A+B Format (20)解题描述
1. 作业链接 2. 解题的思路过程 首先这是道简单的计算题,要求计算a+b的值. 看初值条件,将a和b的取值限制在一个区间内. 本题难点和重点是如何把输出值形成题目要求的格式. 因为负数可通过在前面 ...
- 1001.A+B Format (20) 解题
代码入口(https://github.com/NSDie/object-oriented) 这题的解题思路我有两个: 第一个是两个数字相加然后判断位数,因为题目限制了范围1000000的绝对值以内嘛 ...
- (2016.2.2)1001.A+B Format (20)解题思路
https://github.com/UNWILL2LOSE/object-oriented 解题思路 目标: *首先运算要求实现输入2个数后,输出类似于银行的支票上的带分隔符规则的数字. 代码实现思 ...
- 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 (20 point(s))
题目: 我一开始的思路是: 用math.h中的log10函数来计算位数(不建议这么做,因为会很慢,而且会出一点别的问题): 用pow函数根据要插入分号的位置来拆分a+b成一个个数字(例如res / p ...
- 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 ...
- 关于‘1001.A+B Format (20)’的解题报告
1001.A+B Format(20) 首先要感谢一下指导我github上传问题的小伙伴们,捣腾了一整天我终于摸到了一点门路,真的谢谢你们. 小豪的github 问题描述: Calculate a + ...
- 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 ...
随机推荐
- div设置contenteditable="true" 光标消失:原因
原因1:document.onselectstart= function(){return false;}; 原因2:父层设置了user-select:none 导致 子层设置了 contentedi ...
- jQuery 1.11 / 2.1 beta 版发布
jQuery开发团队近日发布了jQuery 1.11和2.1的beta版本. jQuery 1.x版本支持IE 6/7/8,jQuery 2.x 不支持,如果你已经抛弃了IE 6/7/8用户,可以升级 ...
- [TJOI2013]拯救小矮人
题目 首先有一个很假的贪心 我们定义一个人的需求为\(H-h_i-b_i\),就是这个人需要多少的高度在他下面他才能逃出去 我们趁剩余的高度还够,优先满足需求较高的 显然是错的,可能有一个人身高很高, ...
- ORB SLAM2在Ubuntu 16.04上的运行配置
http://www.mamicode.com/info-detail-1773781.html 安装依赖 安装OpenGL 1. 安装opengl Library$sudo apt-get inst ...
- haproxy安装及日志配置
1.yum安装 yum install haproxy -y 2.配置文件修改,经典配置 vim /etc/haproxy/haproxy.cfg #------------------------- ...
- OpenCV——反向投影(定位模板图像在输入图像中的位置)
反向投影: #include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namesp ...
- JAVA框架 json数据交互
一.导入依赖: <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId& ...
- scala-数组操作
package com.bigdata import scala.collection.mutable.ArrayBuffer object ArrayO { def main(args: Array ...
- 2015306 白皎 《网络攻防》EXP6 信息搜集与漏洞扫描
2015306 白皎 <网络攻防>EXP6 信息搜集与漏洞扫描 一.问题回答 (1)哪些组织负责DNS,IP的管理. 顶级的管理者是Internet Corporation for Ass ...
- 20155308 《网络攻防》 Exp3 免杀原理与实践
20155308 <网络攻防> Exp3 免杀原理与实践 基础问题回答 杀软是如何检测出恶意代码的? 基于特征来检测:恶意代码中一般会有一段有较明显特征的代码也就是特征码,如果杀毒软件检测 ...