大数乘法
Posted 绿水白川
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了大数乘法相关的知识,希望对你有一定的参考价值。
#include <iostream> #include <cstring> using namespace std; #define MAX 1000000 struct Node{ int data; Node* next; }; void output(Node* head){ if(!head->next && !head->data) return; output(head->next); cout << head->data; } void Mul(char *a, char *b){ char *ap = a, *bp = b; Node *head = new Node; head->data = 0; head->next = NULL; Node *p, *q=head, *p1; int temp=0, temp1, bit; while(*bp){ p = q->next; p1 = q; bit = *bp-48; while(*ap || temp){ if(!p){ p = new Node; p->data = 0; p->next = NULL; p1->next = p; } if(*ap == 0) temp1 = temp; else{ temp1 = p1->data + (*ap-48)*bit + temp; ap++; } p1->data = temp1 % 10; temp = temp1 / 10; p1 = p; p = p->next; } ap = a; bp++; q = q->next; } p = head; output(p); cout << endl; while(head){ p = head->next; delete head; head = p; } } int main(){ char a[MAX], b[MAX]; cin.getline(a, MAX, ‘\n‘); cin.getline(b, MAX, ‘\n‘); Mul(strrev(a), strrev(b)); return 0; }
以上是关于大数乘法的主要内容,如果未能解决你的问题,请参考以下文章