Turbo C中两个100位数字的总和 [英] Sum Of Two 100 Digit Numbers in turbo c

查看:64
本文介绍了Turbo C中两个100位数字的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi
怎么了?

例如
10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+
10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
=
20000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

hi
How did it?

for example
10000000000000000000000000000000000000000000000000000000000000000000000000000000000
+
10000000000000000000000000000000000000000000000000000000000000000000000000000000000
=
20000000000000000000000000000000000000000000000000000000000000000000000000000000000

推荐答案

我不知道Turbo C是否支持无符号短整数,所以我将其放在伪代码中:

I don''t know if Turbo C supports unsigned short integers so I''ll just put this down in pseudo code:

integer maxLength = 10; // maximum number of bytes in your integer
unsigend short addend1[maxLength]; // byte order will be LSB on the first index position
unsigend short addend2[maxLength]; // to MSB on the highest index position
unsigned short sum[maxLength];     // ditto for the final sum

unsigned integer carry = 0;

string someBigIntInStringForm    = "199999999999999999999999999923423412312123123123";
string anotherBigIntInStringForm = "000000000000000000000022323288423412312123123123";

addend1 = convertStringToUnsignedShortArray(someBigIntInStringForm);
addend2 = convertStringToUnsignedShortArray(anotherBigIntInStringForm);

for(integer idx = 0; idx < maxLength; idx++)
{
    sum[idx] = (addend1[idx] + addend2[idx] + carry) % 256;
    carry    = (addend1[idx] + addend2[idx] + carry) / 256;
}

if(carry <> 0)
{
    // signal that an overflow error has occurred
}
else
{
    // array sum now contains the sum of addend1 and addend2
}



这绝不是立即可用的解决方案.它应该使您对如何解决此问题有所了解.如果还应包括减法,则需要一种方法将数组转换为 2's补数 [ ^ ].我还省略了从字符串表示形式到二进制表示形式的转换.有趣的是,这听起来可能很有趣:如果您还将用于添加数组的功能放入函数中,则可用于构建字符串形式的二进制表示形式.

干杯!

-MRB



This is in no way a ready to use solution. It should give you an idea though on how one might be able to tackle this problem. If subtraction should also be covered you''d need a method to convert an array into 2''s complement[^]. I also left out the converions from the string representation to the binary representation. Funny as this might sound to you though: If you also put the functionality for adding the arrays into a function, this can be used to build the binary representation of the string form.

Cheers!

-MRB


由于您无法访问任意长的数字,因此最好的方法是手动进行操作.由于这很累,所以我不会做任何代码-您必须自己做一些事情!

问题是将两个数字加在一起,其长度超过了机器的可用数字容量.因此,您需要自己做.用铅笔和纸怎么办?
您需要将它们写下来,以便将最低有效数字排列在一起,然后逐步处理从最低有效数字到最高有效数字的数字,将一对数字加在一起-包括前一对数字中的任何进位-然后分解结果变成一个数字加上零或一的进位(9 + 9 = 18,所以您可以拥有的最大进位是"1").结果是数字序列,如果前一对有进位,则最高有效位是一个.

在纸上尝试:确定您的限制是什么(例如,您将需要在输出中留出足够的空间以容纳与两个输入中的较长者相同的位数,再加上一位,因为您将希望打印输出编号最重要的数字),然后尝试在纸上进行编码.

如果您对此有疑问,那么至少您会遇到一个特定的问题,并且您会学到一种宝贵的技能-如何分解任务并制定出如何做的事情.
Since you don''t have access to arbitrarily long numbers, the best way is going to be to do it manually. Since this smells heavily of homework, I won''t do any code - you have to do something yourself!

The problem is to add two numbers together, the length of which exceeds the available number capacity of the machine. So, you need to do it yourself. How would you do it with a pencil and paper?
You would write them down, so that the least significant digits lined up, then you would work you way through the digits from the least significant to the most significant, adding a pair of digits together - including any carry from the previous pair - then breaking the result into a digit plus a carry of either zero or one (9 + 9 = 18, so the largest carry you can have is "1"). The result is the sequence of digits, with the most significant digit being a one if there was a carry from the previous pair.

Try it on paper: work out what your limits are, (for example, you will need to allow enough space in the output for the same number of digits as the longer of the two inputs, plus one digit because you will want to print the number most significant first) and then try to code what you did on paper.

If you get problems with that, then at least you have a specific problem you can ask about, and you learn a valuable skill - how to break down a task and work out how to do it.


这篇关于Turbo C中两个100位数字的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆