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)解题过程的更多相关文章

  1. "1001. A+B Format (20)" 解题报告

    Github : git@github.com:Circlecos/object-oriented.git PDF Of Markdown : "1001. A+B Format (20)& ...

  2. 1001.A+B Format (20)解题描述

    1. 作业链接 2. 解题的思路过程 首先这是道简单的计算题,要求计算a+b的值. 看初值条件,将a和b的取值限制在一个区间内. 本题难点和重点是如何把输出值形成题目要求的格式. 因为负数可通过在前面 ...

  3. 1001.A+B Format (20) 解题

    代码入口(https://github.com/NSDie/object-oriented) 这题的解题思路我有两个: 第一个是两个数字相加然后判断位数,因为题目限制了范围1000000的绝对值以内嘛 ...

  4. (2016.2.2)1001.A+B Format (20)解题思路

    https://github.com/UNWILL2LOSE/object-oriented 解题思路 目标: *首先运算要求实现输入2个数后,输出类似于银行的支票上的带分隔符规则的数字. 代码实现思 ...

  5. 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 ...

  6. PAT 1001 A+B Format (20 point(s))

    题目: 我一开始的思路是: 用math.h中的log10函数来计算位数(不建议这么做,因为会很慢,而且会出一点别的问题): 用pow函数根据要插入分号的位置来拆分a+b成一个个数字(例如res / p ...

  7. 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 ...

  8. 关于‘1001.A+B Format (20)’的解题报告

    1001.A+B Format(20) 首先要感谢一下指导我github上传问题的小伙伴们,捣腾了一整天我终于摸到了一点门路,真的谢谢你们. 小豪的github 问题描述: Calculate a + ...

  9. 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 ...

随机推荐

  1. jQuery html表格排序插件:tablesorter

    ablesort是一款很好用的jQuery表格排序插件. 支持多种数据类型排序,会自动识别表格内容数据类型,使用也非常方便. 使用jQuery tablesort实现html表格方法: 1. 下载jQ ...

  2. PHP实现微信发红包功能2

    <?php class wxPay { //配置参数信息 const SHANGHUHAO = "1430998xxx";//商户号 const PARTNERKEY = & ...

  3. DJI Mobile SDK 新教程

    DJI Mobile SDK 新教程发布! http://bbs.dji.com/thread-20282-1-1.html Android 如何创建一个航拍相机App: 你将学到如何配置DJI Mo ...

  4. Docker技术入门与实战 第二版-学习笔记-1-镜像

    镜像与容器之间的关系: 镜像(Image)和容器(Container)的关系,就像是面向对象程序设计中的类和实例一样,镜像是静态的定义,容器是镜像运行时的实体.容器可以被 创建.启动.停止.删除.暂停 ...

  5. shiro实战系列(三)之架构

    Apache Shiro 的设计目标是通过直观和易于使用来简化应用程序安全.Shiro 的核心设计体现了大多数人们是如何考虑应用程序安全的——在某些人(或某些事)与应用程序交互的背景下.   应用软件 ...

  6. leetcode18—4Sum

    Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums s ...

  7. Python2.7-datetime

    datetime 模块用于操作日期时间模块内定义了5个类:date,time,datetime,timedelta,tzinfo 1.timedelta对象,代表一个时间间隔datetime.time ...

  8. POJ 2367 Genealogical tree 拓扑排序入门题

    Genealogical tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8003   Accepted: 5184 ...

  9. 【转】php容易犯错的10个地方

    原文地址: http://www.toptal.com/php/10-most-common-mistakes-php-programmers-make 译文地址:http://codecloud.n ...

  10. Linux系统扫描技术及安全防范

    1.概述 一.主机扫描 二.路由扫描 三.批量服务扫描 四.linux防范恶意扫描安全策略 一个典型的网络安全事件 ·案例:通过网络扫描方式获取某运营商核心设备管理权限 step01:通过tracer ...