两个数字数组相乘
谁能告诉我如何在 C 中将两个数字数组相乘?数字数组基本上源自两个包含数字的字符串。例如:123456 和 132465。
编辑:我有两个字符串 S1 = "123456"
和 S2="132546"
。然后我将这两个字符串转换为整数数组,即 int IS1[6] 和 IS2[6],这样
IS1[1] = 1, IS1[2] = 2......
现在
IS2[1] = 1, IS2[2] = 3.....
我必须将这两个数组相乘。请帮忙。
Can anyone please tell me how to multiply two number arrays in C? The number arrays are basically derived from two strings containing digits. eg: 123456 and 132465.
Edit: I had two string as S1 = "123456"
and S2="132546"
. I then converted these two strings into array of ints i.e. int IS1[6] and IS2[6] so that
IS1[1] = 1, IS1[2] = 2......
and
IS2[1] = 1, IS2[2] = 3.....
Now I have to mulitply these two arrrays. Please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
目前还不清楚你到底想乘什么。如果您需要将
char[]
中的两个以 null 结尾的字符串相乘,可以使用atoi
将它们转换为int
值:It's not clear what exactly you want to multiply. If you need to multiply two null terminated strings in a
char[]
, you can convert them toint
values withatoi
:如果您想使用纸笔算术并且不知道该怎么做,这里是 插图。
If you want to use the paper-and-pencil arithmetics and don't know how to do that, here is the illustration.
我只是编写了一个简单的程序,使用算法长乘法将存储在文件中的 2 行中的两个数字相乘。它可以将两个超过 10 亿的数字相乘
示例:
源代码:
请查看并给出您的评论
http://code.google.com/p/juniormultiply /source/browse/#svn/trunk/src
I just code a simple program multiply two number stored in 2 line in file using algorithm long multiplication. It can multiply two number which have more than 1 billion number in each other
Example:
Source code:
Please review and give your comment
http://code.google.com/p/juniormultiply/source/browse/#svn/trunk/src
如果您的数字足够小,请将它们解析为整数(
atoi
)。如果它们太大而无法放入整数:
使用诸如gmp之类的库
之类的库或使用铅笔和纸算法,但您将重新发明轮子.
If your numbers are small enough, parse them into ints (
atoi
).If they are too large to fit into ints:
use a library such as gmp
or use the pencil-and-paper algorithm, but you'll be reinventing the wheel.
好吧,如果你想生成一个包含乘法的数组,你可以使用:
如果你想要内积,这就是你可以做到的:
如果,像前面的答案所暗示的那样,你想要的是将两个数组视为数字您可以使用提到的 atoi() 函数。
Well, if you want to generate an array containing the multiplications, you could use:
If you want the inner product, this is how you could do it:
If, like previous answers suggest, what you wanted was to treat the two arrays as numbers you could use the atoi() function as mentioned.
如果是真实项目,就进行转换。
如果这是算法练习,请根据铅笔和纸的方法进行多重循环。
If it is for a real project, do the conversion.
If this is an exercise of algorithm, do the multiple loop according to the pencil and paper approach.