高精度乘法程序
对于超过20位的数的乘法问题,我们无法使用普通的方法!!!即使是longlong也会超出范围的!
像这样的数,我们只能使用高精度的知识利用数组的方法解决问题!
对于高精度乘法的问题,其实思路和高精度加法的思路差不多,都需要使用
字符数组来存放每次算完的结果!
1 2 3
*4 5 6
________________
12 15 18
8 10 12
4 5 6
_____________________
4 13 28 27 18
观察这个程序不难发现大整数乘法的规律!!!每次算完先不要进位,
先把算玩的结果存到一个2维数组里,最后再求他们的和,求完和之后再进位!!!
最后在逆序输出即可!!哈!!废话不多说,看程序!!!
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char num1[105],num2[105];
int Num1[105],Num2[105];
int Sum[105][105],sum[105];
int i,j;
while(gets(num1))//输入第一个乘数
{
memset(Num1,0,sizeof(Num1));
memset(Num2,0,sizeof(Num2));
memset(Sum,0,sizeof(Sum));//将数组初始化!!
gets(num2);//输入第2个程序!!!
int s1=strlen(num1),
s2=strlen(num2);//求出数组的长度!
for(i=0;i<s1;i++)
Num1[s1-1-i]=num1[i]-'0';
for(j=0;j<s2;j++)
Num2[s2-1-j]=num2[j]-'0';//将字符型数组分别存到整型数组里!!
for(i=0;i<s1;i++)
for(j=0;j<s2;j++)
Sum[i][j+i]=Num1[i]*Num2[j];
for(i=0;i<s1;i++)
{
for(j=i;j<i+s2;j++)
cout<<Sum[i][j];
cout<<endl;
}//依次输出每个中间的加数!!!
int c=0;//用于表示进位!!!!
for(j=0;j<s2+s1-1;j++)
{
int s=0;
for(i=0;i<s1;i++)
s+=Sum[i][j];
sum[j]=(s+c)%10;
c=(s+c)/10;
}//处理进位!!!
for(i=s1+s2-2;i>=0;i--)
cout<<sum[i];//逆序输出每个元素!!!求得两个数的积!!!
cout<<endl;
}
return 0;
}
C语言
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
/* 高精度乘法输入:两行,每行表示一个非负整数(不超过10000位) 输出:两数的乘积。 */ #include <stdio.h> #include <string.h> #include <stdlib.h> #define MAX 10001 int bigchenfa( int *sum, int *a, int *b, int la, int lb) { int i,j,lsum = 0 ; memset (sum,0, sizeof (sum)); for (i=1 ; i<= la ; i++) /*用数组模拟运算*/ for (j=1,lsum=i-1; j<= lb ; j++) sum[++lsum] += b[j] * a[i] ; for (i=1 ; i<= lsum ; i++) /*进位处理*/ if (sum[i] >= 10) { if ( sum[lsum] >= 10) lsum ++ ; sum[i+1] += sum[i] / 10 ; sum[i] %= 10 ; } return lsum ; } int main( void ) { int a[MAX]={0},b[MAX]={0},sum[MAX*2]={0} ; int la=0,lb=0,lsum=0; int i,j ; char sa[MAX],sb[MAX] ; scanf (\"%s %s\",sa,sb); la = strlen (sa); lb = strlen (sb); for (i=1,j=la-1; i<= la ; i++,j--) a[i] = sa[j] - '0' ; for (i=1,j=lb-1; i<= lb ; i++,j--) b[i] = sb[j] - '0' ; lsum = bigchenfa(sum,a,b,la,lb) ; for (i=lsum ; i> 0 ; i--) printf ( "%d" ,sum[i]); printf ( "\n" ); return 0 ; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
#include<stdio.h> int main() { char n[255]={},m[255]={}; int n1[255]={},m1[255]={},s[510]={}; int i,j,k=0,t,x=0,dig; int lenn,lenm; scanf ( "%s%s" ,&n,&m); lenn= strlen (n); lenm= strlen (m); for (i=0;i<lenn;i++) n1[i]=n[i]-48; for (j=0;j<lenm;j++) m1[j]=m[j]-48; for (j=lenm-1;j>=0;j--) { t=k; for (i=lenn-1;i>=0;i--) { s[t]+=n1[i]*m1[j]; t++; } ++k; dig=t; } for (i=0;i<dig;i++) while (s[i]>=10) { s[i]-=10; ++s[i+1]; } if (s[dig]!=0) for (i=dig;i>=0;i--) printf ( "%d" ,s[i]); else for (i=dig-1;i>=0;i--) printf ( "%d" ,s[i]); return 0; } |
Pascal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
var i,j,la,lb,len: integer ; s1,s2: string ; m: longint ; a,b,c: array [ 1..250 ] of integer ; begin readln(s1); la:=length(s1); readln(s2); lb:=length(s2); for i:= 1 to la do a[i]:=ord(s1[la-i+ 1 ])- 48 ; for i:= 1 to lb do b[i]:=ord(s2[lb-i+ 1 ])- 48 ; for i:= 1 to la do for j:= 1 to lb do c[i+j- 1 ]:=c[i+j- 1 ]+a[i]*b[j]; len:=la+lb; for i:= 1 to len do begin c[i+ 1 ]:=c[i+ 1 ]+c[i] div 10 ; c[i]:=c[i] mod 10 ; end ; while c[len]= 0 do dec(len); m:=c[len]; while m> 0 do begin c[len]:=m mod 10 ; m:=m div 10 ; inc(len); end ; for i:=len- 1 downto 1 do write (c[i]); end . |
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
#include <iostream> using namespace std; int main() { int a[100],b[100],c[100],len,la,lb,i,j; long long n,m; cin>>n>>m; la=0; while (n>0) { la++; a[la]=n%10; n=n/10; } lb=0; while (m>0) { lb++; b[lb]=m%10; m=m/10; } memset (c,0, sizeof (c)); for (i=1;i<=la;i++) for (j=1;j<=lb;j++) c[i+j-1]=c[i+j-1]+a[i]*b[j]; len=la+lb; for (i=1;i<=len;i++) { c[i+1]=c[i+1]+c[i]/10; c[i]=c[i]%10; } while (c[len]==0){len--;} m=c[len]; while (m>0) { c[len]=m%10; m=m/10; len++; } for (i=len-1;i>=1;i--) {cout<<c[i];} cout<<endl; return 0; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
#include<cstdio> #include<cstdlib> #include<iostream> #include<cstring> using namespace std; struct node{ int d[100000],l;}; char s[1000000]; node a,b,c; void read(node &x) { scanf ( "%s" ,s); x.l= strlen (s); memset (x.d,0, sizeof (x.d)); for ( int i=0;i<x.l;i++) x.d[x.l-i-1]=s[i]- '0' ; } int main() { read(a);read(b); // if (la>lb) lc=la;else lc=lb; memset (c.d,0, sizeof (c.d)); for ( int i=0;i<a.l;i++) { for ( int j=0;j<b.l;j++) c.d[i+j]+=a.d[i]*b.d[j]; } c.l=a.l+b.l-1; for ( int i=0;i<c.l;i++) { c.d[i+1]+=c.d[i]/10; c.d[i]%=10; } while (c.d[c.l]>0) { c.d[c.l+1]=c.d[c.l]/10; c.d[c.l]%=10; c.l++; } while (c.l>1 && c.d[c.l-1]==0) c.l--; for ( int i=c.l-1;i>=0;i--) printf ( "%d" ,c.d[i]); return 0; } |
vb6.0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
Dim i, j, L(2), a(1000) As Integer , b(1000) As Integer , c(2000, 2000) As Integer , d(2000, 2000) As Integer , x(10000) As Integer , jieguo As String , y(10000) As Integer Private Sub Command1_Click() L(1) = Len(Text2.Text) L(2) = Len(Text3.Text) For i = 1 To L(1) a(i) = Val(Mid(Text2.Text, L(1) - i + 1, 1)) Next i For i = 1 To L(2) b(i) = Val(Mid(Text3.Text, L(2) - i + 1, 1)) Next i For i = 1 To L(2) For j = 1 To L(1) c(i, j) = b(i) * a(j) + c(i, j) d(i, j) = Int(c(i, j) / 10) If d(i, j) > 0 Then c(i, j) = c(i, j) - 10 * d(i, j) c(i, j + 1) = c(i, j + 1) + d(i, j) End If d(i, j) = 0 Next j Next i For i = 1 To L(2) b(i) = 0 Next i For i = 1 To L(1) a(i) = 0 Next i For i = 1 To L(2) For j = 1 To L(1) + 1 x(i + j - 1) = x(i + j - 1) + c(i, j) c(i, j) = 0 Next j Next i For i = 1 To L(1) + L(2) + 1 y(i) = Int(x(i) / 10) If y(i) > 0 Then x(i) = x(i) - 10 * y(i) x(i + 1) = x(i + 1) + y(i) End If y(i) = 0 Next i Text1.Text = "" If x(L(1) + L(2) + 1) <> 0 Then Text1.Text = Text1.Text & x(L(1) + L(2) + 1) If x(L(1) + L(2)) <> 0 Then Text1.Text = Text1.Text & x(L(1) + L(2)) For i = L(1) + L(2) - 1 To 1 Step -1 Text1.Text = Text1.Text & x(i) Next i For i = 1 To L(1) + L(2) + 1 x(i) = 0 Next i L(1) = 0 L(2) = 0 jieguo = Text1.Text End Sub Private Sub Form_Load() Text2.Text = "a" Text3.Text = "b" Text1.Text = "结果" Command1.Caption = "计算" Timer1.Interval = 1 "interval,是间隔,值只能为数字而不能是true jieguo = "结果" End Sub Private Sub Timer1_Timer() Text1.Text = jieguo End Sub |