大整数乘法,似乎不是很健壮,过段时间再编个好的补充上来
2008-04-30 09:48
309 查看
void Reverse(char str[])
{
char tmp;
int length=strlen(str);
int mid=length/2;
int i;
for(i=0;i<mid;i++)
{
tmp=str[i];
str[i]=str[length-i-1];
str[length-i-1]=tmp;
}
}
void Multiply( char a[1000],char b[1000],char res[1000] )//res = a*b
{
Reverse(a);
Reverse(b);
int i,j,k;
int benwei;
int jinwei;
memset(res,'0',1000);
for (j=0;j<strlen(b);j++)
{
jinwei = 0;
for (i=0,k=j;i<strlen(a);i++,k++)
{
int bb = int(b[j]-'0');
int aa = int(a[i]-'0');
int cc = int(res[k]-'0');
benwei = (bb*aa+jinwei+cc)%10;
jinwei = (bb*aa+jinwei+cc)/10;
res[k] = char( '0'+benwei );
}
if (jinwei!=0)
{
res[k] = char( '0'+jinwei );
}
}
for (i=999;res[i]=='0';i--)
{
}
res[i+1]='';
Reverse(res);
Reverse(a);//反转回来
Reverse(b);//反转回来
// cout<<a<<"*"<<b<<"=";
// cout<<res<<endl;
}
{
char tmp;
int length=strlen(str);
int mid=length/2;
int i;
for(i=0;i<mid;i++)
{
tmp=str[i];
str[i]=str[length-i-1];
str[length-i-1]=tmp;
}
}
void Multiply( char a[1000],char b[1000],char res[1000] )//res = a*b
{
Reverse(a);
Reverse(b);
int i,j,k;
int benwei;
int jinwei;
memset(res,'0',1000);
for (j=0;j<strlen(b);j++)
{
jinwei = 0;
for (i=0,k=j;i<strlen(a);i++,k++)
{
int bb = int(b[j]-'0');
int aa = int(a[i]-'0');
int cc = int(res[k]-'0');
benwei = (bb*aa+jinwei+cc)%10;
jinwei = (bb*aa+jinwei+cc)/10;
res[k] = char( '0'+benwei );
}
if (jinwei!=0)
{
res[k] = char( '0'+jinwei );
}
}
for (i=999;res[i]=='0';i--)
{
}
res[i+1]='';
Reverse(res);
Reverse(a);//反转回来
Reverse(b);//反转回来
// cout<<a<<"*"<<b<<"=";
// cout<<res<<endl;
}
相关文章推荐
- 用C++重写String类,实现并不是最完美的,欢迎指正补充!后续还有更多类似的实现放上来, 欢迎关注!!!
- 关于大整数的乘法的算法时间复杂度的计算过程推导(纯属个人推测,请高人指正)
- 长度为n的整数数组,找出其中任意(n-1)个乘积最大的那一组,只能用乘法,不可 以用除法。要求对算法的时间复杂度和空间复杂度作出分析,可以写思路也可以写程序。
- [大整数乘法]分治算法的时间复杂度研究
- Qt自定义sleep延时函数(巧妙的使用时间差,但这样似乎CPU满格,而不是沉睡)
- datetime 时间 显示的是12小时的 下午1点显示的就是1 不是13 .
- 数据结构_分别使用for循环和递归打印1-N的所有整数,比较运行时间
- 【CodeVS 3123】高精度练习之超大整数乘法 &【BZOJ 2197】FFT快速傅立叶
- 大整数乘法(C语言)
- 算法实现(5)大整数乘法
- AtCoder Beginner Contest 085 C Otoshidama(计算机整数乘法问题+方程求解)
- 大整数乘法
- 整数大数模拟 高精度加法 高精度减法 高精度乘法 高精度除法 c/c++ java
- 某整形数组中除了两个单身整数外, 其余的整数都是成对出现的, 利用C/C++代码求出这两个单身整数。 要求: 时间复杂度o(n), 空间复杂度o(1)------某公司招聘试题
- Highcharts 时间截断 利用主轴可以做上下限设定---不是很可取 上下限设定---最佳可取方案
- 证明一个操作的一致性读过程是以自己开始执行的时间为准回滚的不是以该操作所在的事务开始的时间为准回滚的
- 从键盘输入两个数字,之后完成两个整数的加法操作,不是数字提示错误信息
- webpack不是内部命令问题补充(window系统)
- 如何判断一个整数数组中是否有重复元素?要求时间复杂度O(n),空间复杂度O(1)
- DS1302的延时时间要按照datasheet上来