大数除法

ttlb 2003-09-10 07:49:02
请问如何进行用string表示的大数除法
...全文
496 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Beover1984 2003-09-11
  • 打赏
  • 举报
回复
简单说就是用数组分解,我那有一个乘法的源程序,要我给你发过去世
ttlb 2003-09-11
  • 打赏
  • 举报
回复
多谢大家的提醒,我已经做出来了,方法是循环的减法。
cnxiaohai 2003-09-11
  • 打赏
  • 举报
回复
给你一个比较古老的,外国人写的程序~
这只是两个大整数的乘法,你可参照下面的程序.
要是除法的话,可以用数值分析里的牛顿逼近法~~

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Question: *
* How to calculate two integers each one has forty bit. *
* Algorithm: *
* Declare two arrays suppose to be array A and B to storage *
* the two integers just as a intacter singly. *
* Get the first number from array B,then use it to multiply *
* the number of array A singly. Declare another array C to *
* storage the value when multiplying. *
* Thinking about the carrying bits. *
* Thinking: *
* Can you use the Linear_List to do this? *
* If they are two floating numbers,so how to do it? *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <conio.h>

//To calculate two 40 bits integers multiply,change 10 to 40
#define THE_ARR_A_SIZE 10 #define ARR_B_SIZE THE_ARR_A_SIZE
#define ARR_C_SIZE (THE_ARR_A_SIZE + ARR_B_SIZE)

void main(void);
void Multiply( short *, short *, short *, short, short);
void Carrying( short *c, short);

void main(void)
{
short A[THE_ARR_A_SIZE] = {9, 9, 9, 9, 9, 9, 9, 9, 9, 9}; // 10
short B[ARR_B_SIZE] = {9, 9, 9, 9, 9, 9, 9, 9, 9, 9};
short C[ARR_C_SIZE];
short i = 0;

for ( NULL; i < ARR_C_SIZE; i++)
{
*(C+i) = 0;
}

Multiply(A, B, C, THE_ARR_A_SIZE, ARR_B_SIZE);
Carrying(C, ARR_C_SIZE);

printf("\nThe Result is: ");

i = 0;
if (C[0] == 0) ++i; //To delete the primacy zero
for ( NULL; i < ARR_C_SIZE; i++)
{
printf("%d", C[i]);
}
getch();
printf("\n");
}

/*
* NOTICE:
* The Array C is not initialed,you should do that. Here
* the array is supposed to be filled zero.
* Algorithm:
*
*/

void Multiply( short A[], short B[], short C[], short A_size, short B_size)
{
short i = 0, j = 0, k = 1; // Assigned k to 1 to leave C[0]
//be null to storage the carrying.

for (j = 0; j<B_size; j++)
{
for (i = 0; i<A_size; i++)
{
C[k] += B[j] * A[i];
k++;
}
k = 2;
k = k + j;
}
}

// To solve the carrying .

void Carrying(short C[],short C_size)
{
short i, tens_place = 0, units_order = 0;
short temp = 0, end_arr;

end_arr = C_size-1;
for (i = end_arr; i >= 0; i--)
{
temp = C[i];
if (C[i] > 9)
{
tens_place = temp / 10;
units_order = temp % 10;
C[i-1] += tens_place;
C[i] = units_order;
}
}
}



lybapple 2003-09-10
  • 打赏
  • 举报
回复
什么大数?
是整形的吗?
那样子还是比较好办的。
你可以借鉴int型的算法。

start

1.shift the remainder register left 1 bit

2.Subtract the Divisor register from the left half of the Remainder register and
place the result in the left half of the Remainder register.

>>>>test Remainder

if Remainder>=0

3a.Shift the Remainder register to the left ,Seting the new rightmost bit 1

if Remainder <0

3b.Restore the original value by adding the Divisor register to the left half of
the Remainder register and place the sum in the half of the Remainder register.
Also shift the Remainder register to the left ,seting the new rightmost bit to 0

goto 2 if the repetition<32

Done Shift left half of Remainder right 1 bit .


ZhangYv 2003-09-10
  • 打赏
  • 举报
回复
其实善于利用现有资源是很好的习惯,前些天刚讨论过的。
http://expert.csdn.net/Expert/topic/2201/2201068.xml?temp=.2980921
http://expert.csdn.net/Expert/topic/2225/2225228.xml?temp=.5297815
ttlb 2003-09-10
  • 打赏
  • 举报
回复
比如
两个string
"111111111111111111111111111111112222222222222222222222","555"
求它们的商

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧