这是一道腾讯的笔试题,原题有小数点要求。这里只分析下int型的。网上找到一种解法,看过之后表示解法不错,自己敲了一遍并做一些注释。代码在网上随意可以找到,如下:
#include <iostream>
using namespace std;
#define MAX 10000
//链表结点
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 = 0;
//乘数从低位到高位每一位与被乘数的积起始位置逐一++
Node *Linehead = head;
Node *p,*p1;
//temp为相乘的进位,temp1为当前计算的乘数,bbit保存char转化的int数字
int temp = 0,temp1,bbit;
while(*bp) //若乘数不为空
{
p = Linehead->next;
p1 = Linehead;
bbit = *bp-48;
while(*ap||temp) //若被乘数不为空或者有进位
{
if(!p)
{
p = new Node;
p->data =