两个 long long int 相乘 C

发布于 2024-08-13 05:49:01 字数 178 浏览 2 评论 0原文

我正在编写一个 C 程序作为家庭作业的一部分,其中我必须获得两个长数字的乘积,这两个数字被视为字符串。例如:123456789021 和 132456789098。由于它被视为字符串,因此我将它们转换为 long long int 进行乘法。但最终的结果将非常大(我猜比 long long int 还要大)。谁能建议我一种执行此乘法的方法?

I am working on a program in C as a part of Homework in which I have to get the product of two long numbers which are taken as character string. eg: 123456789021 and 132456789098. Since it is taken as a string, I converted them to long long int for the multiplication. But the resulting product will be very large(larger than long long int I guess). Can anyone please suggest me a method to perform this multiplication?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

眼泪也成诗 2024-08-20 05:49:01

一种方法是:考虑如何在纸上手工将这些数字相乘。在 C 中实现此方法。您将必须发现:

  • 如何将整数(表示为字符串)分解为数字
  • 如何将每个数字转换回整数 0 <= d < 10
  • 如何管理数字数组(即,您应该将数组设置多大?)
  • 如何编写您可能需要的循环 实现乘法
  • 如何管理从一位数字到下一位数字的乘积
  • 如何将这些数字转换回字符以进行输出

Here's one approach: Consider how you would multiply these numbers by hand, on paper. Implement this method in C. You will have to discover:

  • how to break up an integer (represented as a string) into digits
  • how to convert each digit back to an integer 0 <= d < 10
  • how to manage arrays of digits (ie. how big should you make the arrays?)
  • how to write the loop(s) you might need to implement multiplication
  • how to manage carrying products from one digit to the next
  • how to convert those digits back to characters for output
心清如水 2024-08-20 05:49:01

通常表示为字节数组的大整数。您可以查看 Microsoft 在 DLR 中的 BigInteger 实现。我认为他们使用了 Knuth 开发的算法

usually big integers represented as byte arrays. You can look at Microsoft's BigInteger implementation in DLR. I think they've used algorithms developed by Knuth

夏日浅笑〃 2024-08-20 05:49:01

检查这个 BigInteger 库来自《七人世界》的非常基本的示例代码

如果您对我用 C 编写的一些自制代码(仅乘法)感兴趣:

////////////////////////////////////////////////////////////////////////////////

Code removed after I checked the home-work tag ;)

///////////////////////////////////////////////////////////////////////////////////////

这在我参加过的一些早期编程竞赛中有效;)但如果您正在寻找更快的乘法算法,您可以实现 Karatsuba 算法,我个人现在在实时比赛中使用这个。

Check this BigInteger library and a very basic sample code from World of Seven.

If you are interested in some of my home cooked codes in C (only multiplication) :

////////////////////////////////////////////////////////////////////////////////

Code removed after I checked the home-work tag ;)

///////////////////////////////////////////////////////////////////////////////////////

This works in some of the earlier programming contests I had participated ;)But if you are looking for even faster multiplication algorithm you can Implement Karatsuba algorithm,I personally use this now in real time contest.

浪漫人生路 2024-08-20 05:49:01

嘿伙计,看看这个,我昨天刚刚完成它作为我作业的一部分:

#include<stdio.h>
#include<string.h>

int main()
{
    char one[195];
    char two[195];
    char temp[195];
    int a[195],c[195],b[195];
    int x,i,j,k,l,p,add;

    for(;;)/*determining the larger number...*/
    {
        printf("Input A:");
            gets(one);
        printf("Input B:");
            gets(two);

        k=strlen(one);
        l=strlen(two);
        if(l>k)
        {
            strcpy(temp,one);
            strcpy(one,two);
            strcpy(two,temp);
            break;
        }
        else
        {
            break;
        }
    }
        k=strlen(one);
        l=strlen(two);
    for(p=0;p<195;p++)/*assigning all initial values to 0*/
    {
        a[p]=0;
        b[p]=0;
        c[p]=0;
    }

    for(i=0;one[i];i++)/*converting char to integer(note:1,as a character assigned as 49.)*/
    {
        a[i]=((one[--k])-48);
    }

    for(i=0;i<two[i];i++)
    {
        b[i]=((two[--l])-48);
    }


    for(i=0;i<=strlen(two);i++)/*main algorithm*/
    {
        add=0;
        p=0;
        for(j=i;j<=(2*strlen(one)-1);j++)
        {
            x=c[j]+b[i]*a[p]+add;
            c[j]=x%10;
            add=x/10;
            p++;
        }
    }

    printf("\nMultiplication:");
    for(p=(2*strlen(one)-1);p>=0;p--)
    {
        if(p>strlen(one)&&c[p]==0)
        {
            continue;
        }
        printf("%d",c[p]);
    }
    printf("\n");
}

Hey man,check this out,i just completed it yester day as a part of my homework:

#include<stdio.h>
#include<string.h>

int main()
{
    char one[195];
    char two[195];
    char temp[195];
    int a[195],c[195],b[195];
    int x,i,j,k,l,p,add;

    for(;;)/*determining the larger number...*/
    {
        printf("Input A:");
            gets(one);
        printf("Input B:");
            gets(two);

        k=strlen(one);
        l=strlen(two);
        if(l>k)
        {
            strcpy(temp,one);
            strcpy(one,two);
            strcpy(two,temp);
            break;
        }
        else
        {
            break;
        }
    }
        k=strlen(one);
        l=strlen(two);
    for(p=0;p<195;p++)/*assigning all initial values to 0*/
    {
        a[p]=0;
        b[p]=0;
        c[p]=0;
    }

    for(i=0;one[i];i++)/*converting char to integer(note:1,as a character assigned as 49.)*/
    {
        a[i]=((one[--k])-48);
    }

    for(i=0;i<two[i];i++)
    {
        b[i]=((two[--l])-48);
    }


    for(i=0;i<=strlen(two);i++)/*main algorithm*/
    {
        add=0;
        p=0;
        for(j=i;j<=(2*strlen(one)-1);j++)
        {
            x=c[j]+b[i]*a[p]+add;
            c[j]=x%10;
            add=x/10;
            p++;
        }
    }

    printf("\nMultiplication:");
    for(p=(2*strlen(one)-1);p>=0;p--)
    {
        if(p>strlen(one)&&c[p]==0)
        {
            continue;
        }
        printf("%d",c[p]);
    }
    printf("\n");
}
还给你自由 2024-08-20 05:49:01

您可以使用大整数算术库,维基百科此处有一个列表。

You can use a library for large integer arithmetik, Wikipedia has a list here.

但可醉心 2024-08-20 05:49:01

另一种方法是将数字作为浮点数/双精度数相乘,并在显示结果时去掉小数点。

Another approach would be to multiply the numbers as float/double and stip off the decimal when displaying the results.

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