两个大数相乘

问题:两个100位以内的大数相乘

分析:模拟乘法累加法(虽然还有很多其他优秀算法,但是我认为这种是比较好理解且代码量不大的一种),用C语言实现的

参考:https://blog.csdn.net/u010983881/article/details/77503519

代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void mul(char *a,char *b)
{
    int alen,blen,len,i,j,cf;
    int *s;
    alen=strlen(a);
    blen=strlen(b);
    cf=0;   //进位标识
    s=(int*)malloc(sizeof(int)*(alen+blen));
    for(i=0; i<(alen+blen); i++)
    {
        s[i]=0;
    }
    for(i=alen-1; i>=0; i--)
    {
        for(j=blen-1; j>=0; j--)
        {
            s[(alen-1-i)+(blen-1-j)]=(a[i]-'0')*(b[j]-'0')+s[(alen-1-i)+(blen-1-j)];
        }
    }
    for(i=0; i<=(alen+blen-1); i++)
    {
        s[i]=s[i]+cf;
        if(s[i]>=10)
        {
            cf=s[i]/10;
            s[i]=s[i]%10;
        }
        else
        {
            s[i]=s[i];
            cf=0;
        }
    }
    len=0;   //高位可以不输出的0尽量不输出
    for(i=alen+blen-1;i>=0;i++)
    {
        if(s[i]==0)
        {
            len++;
        }
        else
        {
            break;
        }
    }

    len=alen+blen-1-len;
    for(i=len; i>=0; i--)
    {
        printf("%d",s[i]);
    }
    printf("\n");
}
int main()
{
    char n[101],m[101];
    while((scanf("%s %s",n,m))!=EOF)
    {
        mul(n,m);
    }
    return 0;
}


运行结果:

一小点想法:因为两个一位数相乘最大是81,然后如果有100位9与100位9相乘,那么最大的数字应该不超过81+81+16+98=276,是在int类型的范围类,所以保存结果的数组s定义成int类型应该是够用的。

关于C语言里的基本类型以及范围,可以看这个博客:https://blog.csdn.net/qq_16234613/article/details/77541722

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基于链表的 C 语言代码,用于计算两个大数的乘积。 ```c #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *next; }; typedef struct node Node; Node *create_node(int num) { Node *new_node = (Node *) malloc(sizeof(Node)); new_node->data = num; new_node->next = NULL; return new_node; } Node *insert_at_end(Node *head, int num) { Node *new_node = create_node(num); if (head == NULL) { head = new_node; return head; } Node *temp = head; while (temp->next != NULL) { temp = temp->next; } temp->next = new_node; return head; } Node *reverse_list(Node *head) { Node *prev = NULL, *current = head, *next = NULL; while (current != NULL) { next = current->next; current->next = prev; prev = current; current = next; } return prev; } Node *multiply_lists(Node *head1, Node *head2) { Node *result = NULL; Node *temp1 = head1, *temp2; int carry = 0, sum; while (temp1 != NULL) { temp2 = head2; Node *new_node = NULL; carry = 0; while (temp2 != NULL) { sum = (temp1->data * temp2->data) + carry; carry = sum / 10; new_node = insert_at_end(new_node, sum % 10); temp2 = temp2->next; } if (carry > 0) { new_node = insert_at_end(new_node, carry); } new_node = reverse_list(new_node); for (int i = 0; i < temp1->data; i++) { new_node = insert_at_end(new_node, 0); } result = add_lists(result, new_node); temp1 = temp1->next; } return result; } Node *add_lists(Node *head1, Node *head2) { Node *result = NULL; Node *temp1 = head1, *temp2 = head2; int carry = 0, sum; while (temp1 != NULL || temp2 != NULL) { sum = carry + (temp1 != NULL ? temp1->data : 0) + (temp2 != NULL ? temp2->data : 0); carry = sum / 10; result = insert_at_end(result, sum % 10); if (temp1 != NULL) { temp1 = temp1->next; } if (temp2 != NULL) { temp2 = temp2->next; } } if (carry > 0) { result = insert_at_end(result, carry); } return result; } void display_list(Node *head) { Node *temp = head; while (temp != NULL) { printf("%d", temp->data); temp = temp->next; } printf("\n"); } int main() { Node *num1 = NULL, *num2 = NULL, *result = NULL; char c; int num; printf("Enter first number:\n"); while ((c = getchar()) != '\n') { num = c - '0'; num1 = insert_at_end(num1, num); } printf("Enter second number:\n"); while ((c = getchar()) != '\n') { num = c - '0'; num2 = insert_at_end(num2, num); } num1 = reverse_list(num1); num2 = reverse_list(num2); result = multiply_lists(num1, num2); printf("Result:\n"); display_list(result); return 0; } ``` 该代码将两个大数存储为链表,并使用一个辅助函数来反转链表。然后,它将两个链表相乘,产生一个新的链表,该链表包含结果。最后,它将结果链表打印到控制台上。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值