【C语言】超大数乘法运算
昨天做排列组合的时候遇到A(a,b)这个问题,需要计算A(20,20)超大,计算机32位的,最大数只能是2^32,这让我很悲伤!
于是乎就自己研究了如何进行超大数的计算!
/********************************************************************************* * Copyright: (C) 2013 Chen ZhenWei<ieczw@qq.com> * All rights reserved. * * Filename: bignum.c * Description: This file * * Version: 1.0.0(12/12/2013~) * Author: Chen ZhenWei <ieczw@qq.com> * ChangeLog: 1, Release initial version on "12/12/2013 08:21:23 AM" * ********************************************************************************/ #include <stdio.h> #include <string.h> int save_num(const char *str,unsigned short *num) { int i=0; int length = strlen(str); for(i=0; i<length/2; i++) { // printf("%c == %c\n",str[length-1-i*2],str[length-2-i*2]); num[i] = (str[length-1-i*2]-'0')+10*(str[length-2-i*2]-'0'); } if(length%2 != 0) { num[i] = str[0] - '0'; return i+1; } return i; } void main() { unsigned short numa[100]; unsigned short numb[100]; unsigned short result[200]; unsigned short carry; int i=0,j=0,lena,lenb,res_len; char strnum[64]; char flag; scanf("%s",strnum); lena = save_num(strnum,numa); //scanf("%c",&flag); scanf("%s",strnum); lenb = save_num(strnum,numb); memset(result,0x0,200*2); for(i=0; i<lena; i++) for(j=0; j<lenb; j++) { carry = numa[j] * numb[i]; result[j+i] += carry%100; result[j+i+1] += carry/100; } res_len = j+i+1; for(i=0; i<res_len; i++) { if(result[i]>=100) { result[i+1] += result[i]/100; result[i] = result[i]%100; } } for(i=res_len; i>0; i--) { printf("%02d",result[i-1]); } printf("\n"); }
设计思路:
操作结果:
posted on 2013-12-12 15:57 love so much 阅读(770) 评论(0) 编辑 收藏 举报