这是一道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
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。上课去了
分享到:
相关推荐
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 分) 本文将详细介绍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.
(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...
Description Calculate a+b Input Two integer a,b (0<=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编程实现的计算三维二值体积的骨架算法。在图像处理和计算机视觉领域,骨架化是提取物体主干结构的一种方法,它能将物体...
本项目“Calculate-the-day-of-the-year.zip_The Year”提供了这样的学习资源,通过一个名为“Calculate the day of the year.txt”的文本文件,帮助我们理解如何实现这一功能。 首先,我们需要知道的是年份、月份...
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...
【如何计算运放噪声】 运算放大器(Op Amp)在信号处理系统中广泛使用,而其噪声性能对系统的整体性能至关重要。噪声可以来源于多个因素,包括输入电压噪声、电流噪声和电阻噪声等。理解并计算这些噪声对于优化设计...
SRIM,全称为Stopping and Range of Ions in Matter,是一组用于计算离子在物质中停止和射程的程序。这个工具特别关注离子与原子间的碰撞,采用完整的量子力学处理方式来描述这一过程。SRIM始终将移动的原子视为...
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 ...
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 ...
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 ...
// 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...
// Calculate the double of aa -> a * 2; // or simply without type(a, b) -> a + b; // Sum of 2 parameters如果 lambda 不止一个表达式,我们可以{ }使用return(x, y) -> { int sum = x + y; int avg = sum / 2...
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 ...
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....
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 ...