大数相乘
2011-09-15 17:17
190 查看
#include <stdio.h> #include <assert.h> void BigNumMultiply(const char *str1, const char *str2, char *product) { assert(str1 != NULL && str2 != NULL && product != NULL); int i, j; int len1 = (int)strlen(str1); int len2 = (int)strlen(str2); int *dest = (int*)malloc(sizeof(int)*(len1+len2+1)); for (i=0; i<len1+len2+1; i++) { dest[i] = 0; } for (i=0; i<len1; i++) { for (j=0; j<len2; j++) { dest[i+j+1] += (str1[i]-'0')*(str2[j]-'0'); } } for (i=len1+len2-1; i>=0; i--) { // 当i=0时dest[0]=0,这个条件不成立,所以不用担心dest[-1] if (dest[i] >= 10) { dest[i-1] += dest[i]/10; dest[i] %= 10; } product[i] = dest[i]+'0'; } if (product[0] == '0') { i = 1; while (product[i] != '\0') { product[i-1] = product[i]; i++; } product[i-1] = '\0'; } free(dest); return; } int main(void) { char product[50] = {0}; BigNumMultiply("234324", "54651", product); printf("%s\n", product); getchar(); return 0; }
相关文章推荐
- Java笔试题:两个大数相乘
- 大数相乘+大数相加
- 大数相乘
- 大数相乘的算法实现
- 分治--大数相乘
- 大数相乘解决办法,用字符串表示的大数
- 两个大数相乘
- 大数相乘
- 【java】BigDecimal、BigInteger、大数相乘、数值的整数次方、快速乘法
- C语言大数相乘问题普通算法->acm.scu.edu.cn:1002
- 大数相乘算法
- 俩个大数相乘(java实现)
- JS实现大数(整数)相乘
- 2017.8.1拼多多内推内推笔试题(2)—大数相乘
- 大数相乘
- 关于大数相乘问题(Java实现)
- 阶乘问题(大数阶乘)简单 n! (一个大数与一个小数相乘的算法 、一个大数与一个小数的除法算法 *【模板】 )
- 【编程题m_0018】大数相乘 3ff8
- 大数相乘
- 链表实现大数相加和大数相乘