`
tudusi
  • 浏览: 1085353 次
文章分类
社区版块
存档分类
最新评论

calculate the answer of a + b

 
阅读更多
   这是一道acm练习题,原题见“北京理工大学2012年ACM寒假练习题”,http://acm.bit.edu.cn/mod/programming/view.php?a=485

Problem Description
Now let’s calculate the answer of a + b ~
Input
The input will consist of a set of pairs of integers for a and b(-10^1000 <= a, b <= 10^1000). The input is ended by EOF.
Output
For each test case you should output the answer of a + b.
Sample Input
1 1
1 -1
Sample Output
2

0

   看是很简单的题目,确花费了我一个晚上的时间才解出来,关键问题是:

     1.这里面输入的数字远远超过了整形数据的最大范围,要求是-10^1000 <= a, b <= 10^1000,所以只能用字符串表示数字,就涉及到大整数运算问题
     2.所有的测试用例都是隐藏的,我们看不到,所以必须考虑到所有可能的情况,比如 +0、-0、+123......
     3.写好的程序可以在线提交http://acm.bit.edu.cn/mod/programming/submit.php?a=485,这里是在线评测系统,有内存和计算时间限制(不能超过1s)

    以下是本人的程序,仅供参考,速度有些低,算法很粗超,时间紧迫就没有添加注释,而且后期会做重大优化:


2012-4.28编辑:

上面的算法有个最大的弊端就是运行需要占很大的内存,而且时间复杂度很好,但是易于理解,经过与其他同学的交流发现了一种更加简洁的方法:利用补码运算实现大数加法,其原理是模拟二进制补码运算,因为二进制数的减法可以通过对其补码求和实现,这样就可以把减法转换为加法运算,节省了大量的代码,也降低了时间复杂度和空间复杂度,请看代码:


更详细的原理请参考http://blog.csdn.net/jcwkyl/article/details/4090837

。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。上课去了再见

分享到:
评论

相关推荐

    杭电ACM 1002

    Given two integers A and B, your job is to calculate the Sum of A + B. Input The first line of the input contains an integer T(1) which means the number of test cases. Then T lines follow, each ...

    pta_1001 A+B Format (20 分).docx

    pta_1001 A+B Format (20 分) 本文将详细介绍pta_1001 A+B Format的解决方案,该问题涉及到基本的C++编程技术和算法思想。 标题解释 pta_1001 A+B Format (20 分)是指在C++编程中,实现一个简单的加法运算,并将...

    程序设计题目

    Calculate A + B. Each line will contain two integers A and B. Process to end of file. For each case, output A + B in one line.

    SSD7 选择题。Multiple-Choice

    (b) the name of the table, the names of the table's attributes, the data types of the table's attributes, the formats of the table's attributes, and the maximum number of rows that the table can have...

    A+B Problem

    Description Calculate a+b Input Two integer a,b (0&lt;=a,b) Output Output a+b Sample Input 1 2 Sample Output 3

    八大输入输出格式

    1. **A+B for Input-Output Practice (I)** 这种类型的问题通常没有明确说明数据组的数量,我们需要通过输入数据直到遇到EOF(文件结束)来判断何时停止。例如: ```cpp while (scanf("%d%d", &a, &b) != EOF) {...

    MATLAB code to calculate the 3D skeleton of a binary volume

    标题中的“MATLAB code to calculate the 3D skeleton of a binary volume”指的是使用MATLAB编程实现的计算三维二值体积的骨架算法。在图像处理和计算机视觉领域,骨架化是提取物体主干结构的一种方法,它能将物体...

    Calculate-the-day-of-the-year.zip_The Year

    本项目“Calculate-the-day-of-the-year.zip_The Year”提供了这样的学习资源,通过一个名为“Calculate the day of the year.txt”的文本文件,帮助我们理解如何实现这一功能。 首先,我们需要知道的是年份、月份...

    The joint density of the maximum and its location

    It is of interest' to calculate the distribution of the value of the difference between the maximum and present values of a stock or other security along with the time of occurrence of the maximum. At...

    How to calculate the noise of Amplifier.pdf

    【如何计算运放噪声】 运算放大器(Op Amp)在信号处理系统中广泛使用,而其噪声性能对系统的整体性能至关重要。噪声可以来源于多个因素,包括输入电压噪声、电流噪声和电阻噪声等。理解并计算这些噪声对于优化设计...

    SRIM介绍SRIM is a group of programs which calculate the stopping and range of ions into matter

    SRIM,全称为Stopping and Range of Ions in Matter,是一组用于计算离子在物质中停止和射程的程序。这个工具特别关注离子与原子间的碰撞,采用完整的量子力学处理方式来描述这一过程。SRIM始终将移动的原子视为...

    c++实验三、四.pdf

    The class also has a function to calculate the area of the circle, a function to display the basic information of the circle, and a function to modify the basic information of the circle. Rectangle ...

    二级减速器课程设计说明书reducer design specification.doc

    According to the selected fullload speed of the motor nm and the drive shaft speed of the motor nw,we can calculate the total transmission ratio of the transmission device ia: (2)Allocate ...

    Practical Guide to the Packaging of Electronics, Second Edition

    concerned with the housing of the electronics (from component housing to PCB to enclosure and finally to the rack) as well as the ability of this housing to maintain its integrity under various ...

    计算机网络第六版答案

    An ISP earns its money by charging each of the the ISPs that connect to the IXP a relatively small fee, which may depend on the amount of traffic sent to or received from the IXP. 15. Google's ...

    c++实验代码

    // DON'T summit the answer of this question. There are different pricing methods according to different vehiecles. Write a program to calculate freight with demands as below: a) Define Vehicle class...

    Java 8+ 函数式编程速查表.zip

    // Calculate the double of aa -&gt; a * 2; // or simply without type(a, b) -&gt; a + b; // Sum of 2 parameters如果 lambda 不止一个表达式,我们可以{ }使用return(x, y) -&gt; { int sum = x + y; int avg = sum / 2...

    Java邮件开发Fundamentals of the JavaMail API

    the source of a lot of confusion. Much of what people are familiar with when using POP, like the ability to see how many new mail messages they have, are not supported by POP at all. These ...

    spectral hashing

    By utilizing recent results on convergence of graph Laplacian eigenvectors to the Laplace-Beltrami eigenfunctions of manifolds, we show how to efficiently calculate the code of a novel data-point....

    微软内部资料-SQL性能优化2

    For a process to access 3 GB of address space, the executable image must have been linked with the /LARGEADDRESSAWARE flag or modified using Imagecfg.exe. It should be pointed out that SQL Server was ...

Global site tag (gtag.js) - Google Analytics