大数相乘的算法实现
2013-10-23 12:02
671 查看
需求:
2个大的整数(超过计算机内置的整数类型的表示范围,这里用字符数组表示)相乘,如下所示
"234232398382893809203900923093290" * "23"。
计算其结果。
基本思想为把字符数组转化为int数组或者是byte数组(用byte即可,因为只需要存储0-9之间的数字)。然后两个乘数因子的每位数相乘,在相加、进位,即可得到最终的结果。
举例说明如下:234*45
234
*45
————
首先234的每一位乘以5(个数位)得到的结果依次为:
10 15 20
234的每一位乘以4(十数位)得到的结果依次为:
8 12 16
依次得到的每位的结果相加为:
0 10 15 20
8 12 16 0
————————
8 22 31 20
接下来,进行进位的处理:
首先是个位数进位到十位,结果为:
8 22 33 0
十位进位到百位,结果为:
8 25 3 0
百位进位到千位,结果为:
10 5 3 0
千位进位到万位,结果为:
1 0 5 3 0
所以最终的结果为:10530。该结果存储在一个int数组或者是byte数组中,依次输出即可。
C语言实现:
View Code
运行结果示例:
input number 1:2222222222222222222222222222222222222222222222222
input number 2:33333
2222222222222222222222222222222222222222222222222 * 33333 = 74073333333333333333333333333333333333333333333325926
2个大的整数(超过计算机内置的整数类型的表示范围,这里用字符数组表示)相乘,如下所示
"234232398382893809203900923093290" * "23"。
计算其结果。
基本思想为把字符数组转化为int数组或者是byte数组(用byte即可,因为只需要存储0-9之间的数字)。然后两个乘数因子的每位数相乘,在相加、进位,即可得到最终的结果。
举例说明如下:234*45
234
*45
————
首先234的每一位乘以5(个数位)得到的结果依次为:
10 15 20
234的每一位乘以4(十数位)得到的结果依次为:
8 12 16
依次得到的每位的结果相加为:
0 10 15 20
8 12 16 0
————————
8 22 31 20
接下来,进行进位的处理:
首先是个位数进位到十位,结果为:
8 22 33 0
十位进位到百位,结果为:
8 25 3 0
百位进位到千位,结果为:
10 5 3 0
千位进位到万位,结果为:
1 0 5 3 0
所以最终的结果为:10530。该结果存储在一个int数组或者是byte数组中,依次输出即可。
C语言实现:
#include <stdio.h> #include <stdlib.h> #include <string.h> #define N 100 void ConvertToIntArray(char *s, int *a, int len); void multiply(int *a, int *b, int *c, int len1, int len2); void main() { // the max length of operator is limited to 100 char s1 , s2 ; printf("input number 1:"); scanf("%s", s1); printf("input number 2:"); scanf("%s", s2); int a , b ; int i,j; int len1, len2; //get the string's length //printf("%d\n", sizeof(s1)); //printf("%d\n", strlen(s1)); len1 = strlen(s1); len2 = strlen(s2); ConvertToIntArray(s1, a, len1); ConvertToIntArray(s2, b, len2); int c[2*N]; //clear the result array for(i=0; i<2*N; i++) { c[i] = 0; } // multiply a and b to c multiply(a, b, c, len1, len2); for(i=2*N-1; i>=0 && c[i]==0; i--); printf("\n\n"); printf("%s * %s = ", s1, s2); for(j=i; j>=0; j--) { printf("%d", c[j]); } printf("\n\n"); } /* * for example, ['2','3','4'] -> [4, 3, 2] * */ void ConvertToIntArray(char *s, int *a, int len) { int i = 0, j = 0; for(i=len - 1; i>=0; i--) { a[j] = s[i] - '0'; j++; } } void multiply(int *a, int *b, int *c, int len1, int len2) { int i,j; for(i=0; i<len1; i++) { for(j=0; j<len2; j++) { c[i+j] += a[i] * b[j]; } } for(i=0;i<10;i++) //for 234*45, the temporary result is c={20,31,22,8,0,0,...} printf("c[%d] = %d ", i, c[i]); //handle carries, and get the final result for(i=0; i<2*N; i++) { c[i+1] += c[i]/10; c[i] = c[i]%10; } for(i=0; i<10; i++) //for 234*45, the result is c={0,3,5,0,1,0,0,0,0,0}, you can get the result 10530 just output the array verversely and ignore the previous zero printf("c[%d] = %d ", i, c[i]); }
View Code
运行结果示例:
input number 1:2222222222222222222222222222222222222222222222222
input number 2:33333
2222222222222222222222222222222222222222222222222 * 33333 = 74073333333333333333333333333333333333333333333325926
相关文章推荐
- mkdir命令
- 基于MySQL的数据库集群系统的实现
- 怎么在RHEL 6.2上安装启用ipmitool
- alert(++[[]][+[]]+[+[]])
- 了解黑客常用的工具,我也能反黑了
- java学习之类与对象1
- Documentation/io-mapping.txt
- linux cut命令参数说明,以及实例
- linux cut命令参数说明,以及实例
- pwd命令
- php遍历数组什么时候作用在拷贝上?
- css
- Documentation/xtensa/mmu.txt
- 推荐:Android 网络通信框架Volley简介(Google IO 2013)
- 字符串格式化操作 目前见过最全的
- hive的UDF 函数
- cd命令
- rhel6-HAproxy+keepalived
- repeater绑定的时候的小技巧(调用后台方法对当前绑定项进行进一步处理)
- 解析XML出现java.net.MalformedURLException: unknown protocol: e