两个数字数组相乘

发布于 2024-08-13 10:20:38 字数 329 浏览 9 评论 0原文

谁能告诉我如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

安人多梦 2024-08-20 10:20:38

目前还不清楚你到底想乘什么。如果您需要将 char[] 中的两个以 null 结尾的字符串相乘,可以使用 atoi 将它们转换为 int 值:

int result = atoi(str1) * atoi(str2);

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 to int values with atoi:

int result = atoi(str1) * atoi(str2);
绅刃 2024-08-20 10:20:38

如果您想使用纸笔算术并且不知道该怎么做,这里是 插图

If you want to use the paper-and-pencil arithmetics and don't know how to do that, here is the illustration.

谢绝鈎搭 2024-08-20 10:20:38

我只是编写了一个简单的程序,使用算法长乘法将存储在文件中的 2 行中的两个数字相乘。它可以将两个超过 10 亿的数字相乘

示例:

            23958233
            5830 ×
         ------------
            00000000  ( =      23,958,233 ×     0)
           71874699   ( =      23,958,233 ×    30)
          191665864   ( =      23,958,233 ×   800)
         119791165    ( =      23,958,233 × 5,000)

源代码:

请查看并给出您的评论
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:

            23958233
            5830 ×
         ------------
            00000000  ( =      23,958,233 ×     0)
           71874699   ( =      23,958,233 ×    30)
          191665864   ( =      23,958,233 ×   800)
         119791165    ( =      23,958,233 × 5,000)

Source code:

Please review and give your comment
http://code.google.com/p/juniormultiply/source/browse/#svn/trunk/src

半衾梦 2024-08-20 10:20:38

如果您的数字足够小,请将它们解析为整数(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.

避讳 2024-08-20 10:20:38

好吧,如果你想生成一个包含乘法的数组,你可以使用:

int *a, *b, *c; // pointers to arrays of size n
for (unsigned i=0;i<n;++i)
  c[i] = a[i] * b[i];

如果你想要内积,这就是你可以做到的:

int *a, *b; // pointers to arrays of size n
int res = 0;
for (unsigned i=0;i<n;++i)
  res += a[i] * b[i];

如果,像前面的答案所暗示的那样,你想要的是将两个数组视为数字您可以使用提到的 atoi() 函数。

Well, if you want to generate an array containing the multiplications, you could use:

int *a, *b, *c; // pointers to arrays of size n
for (unsigned i=0;i<n;++i)
  c[i] = a[i] * b[i];

If you want the inner product, this is how you could do it:

int *a, *b; // pointers to arrays of size n
int res = 0;
for (unsigned i=0;i<n;++i)
  res += a[i] * b[i];

If, like previous answers suggest, what you wanted was to treat the two arrays as numbers you could use the atoi() function as mentioned.

撩心不撩汉 2024-08-20 10:20:38

如果是真实项目,就进行转换。

如果这是算法练习,请根据铅笔和纸的方法进行多重循环。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文