博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
大数除法
阅读量:5308 次
发布时间:2019-06-14

本文共 4757 字,大约阅读时间需要 15 分钟。

#include 
#include
#include
#include
#define MaxSize 10000bool _DividendIsLarger(int *Dividend,int *Divisor,int DividendLen,int DivisorLen){ int DivisorLenEnd; if(DividendLen < DivisorLen) { return false; } else if(DividendLen == DivisorLen) { for(DivisorLenEnd = DividendLen - 1;DivisorLenEnd >= 0;DivisorLenEnd --) { if(Dividend[DivisorLenEnd] < Divisor[DivisorLenEnd]) { return false; } } } return true;}//sub's result store in Dividend[],return the length of Dividend[]int _TwoBigNumSub(int *Dividend,int *Divisor,int DividendLen,int DivisorLen){ //judge if Dividend > Divisor,else return -1 bool Judger = _DividendIsLarger(Dividend,Divisor,DividendLen,DivisorLen); if(! Judger) { return -1; } //do the Sub int DivisorEnd; for(DivisorEnd = 0;DivisorEnd < DividendLen;DivisorEnd ++) { Dividend[DivisorEnd] -= Divisor[DivisorEnd]; if(Dividend[DivisorEnd] < 0) { Dividend[DivisorEnd] += 10; Dividend[DivisorEnd+1] --; } } for(DivisorEnd = DividendLen-1;DivisorEnd >= 0;DivisorEnd --) { if(Dividend[DivisorEnd]) { return (DivisorEnd + 1); } } return 0;}char *TwoBigNumDivision(char *InputDividend,char *InputDivisor){ int *TempResult = malloc(MaxSize*sizeof(int)); memset(TempResult,0,MaxSize*sizeof(int)); char *Result = malloc(MaxSize*sizeof(char)); memset(Result,0,MaxSize*sizeof(char)); int ResultEnd = 0; int DividendLen = strlen(InputDividend); int DivisorLen = strlen(InputDivisor); int Dividend[MaxSize]; int Divisor[MaxSize]; memset(Dividend,0,sizeof(Dividend)); memset(Divisor,0,sizeof(Divisor)); //reverse to store int InputDivisorEnd,DivisorEnd; for(InputDivisorEnd = DividendLen-1,DivisorEnd = 0;InputDivisorEnd >= 0;InputDivisorEnd --) { Dividend[DivisorEnd ++] = InputDividend[InputDivisorEnd] - '0'; } for(InputDivisorEnd = DivisorLen-1,DivisorEnd = 0;InputDivisorEnd >= 0;InputDivisorEnd --) { Divisor[DivisorEnd ++] = InputDivisor[InputDivisorEnd] - '0'; } //special discuss for zero && one time sub if(! _DividendIsLarger(Dividend,Divisor,DividendLen,DivisorLen)) { Result[ResultEnd++] = '0'; Result[ResultEnd++] = '\0'; return Result; } DividendLen = _TwoBigNumSub(Dividend,Divisor,DividendLen,DivisorLen); if(DividendLen == -1) { Result[ResultEnd++] = '0'; Result[ResultEnd++] = '\0'; return Result; } else if(DividendLen == 0) { Result[ResultEnd++] = '1'; Result[ResultEnd++] = '\0'; return Result; } TempResult[0] ++; int ZeroCompletion = DividendLen - DivisorLen; if(ZeroCompletion < 0) { Result[ResultEnd++] = '1'; Result[ResultEnd++] = '\0'; return Result; } else if(ZeroCompletion > 0) { for(DivisorEnd = DividendLen - 1;DivisorEnd >= 0;DivisorEnd --) { if(DivisorEnd >= ZeroCompletion) { Divisor[DivisorEnd] = Divisor[DivisorEnd - ZeroCompletion]; } else { Divisor[DivisorEnd] = 0; } } } int Temp; DivisorLen = DividendLen; for(DivisorEnd = 0;DivisorEnd <= ZeroCompletion;DivisorEnd ++) { while( (Temp = _TwoBigNumSub(Dividend,Divisor+DivisorEnd,DividendLen,DivisorLen-DivisorEnd)) >= 0) { DividendLen = Temp; TempResult[ZeroCompletion-DivisorEnd] ++; } } //output for(DivisorEnd = 0;DivisorEnd < MaxSize;DivisorEnd ++) { if(TempResult[DivisorEnd] >= 10) { TempResult[DivisorEnd+1] += TempResult[DivisorEnd]/10; TempResult[DivisorEnd] %= 10; } } for(DivisorEnd = MaxSize-1;DivisorEnd >= 0;DivisorEnd --) { if(TempResult[DivisorEnd] != 0) { break; } else if(DivisorEnd == 0 && TempResult[DivisorEnd] == 0) { Result[ResultEnd++] = '0'; Result[ResultEnd++] = '\0'; return Result; } } for(;DivisorEnd >= 0;DivisorEnd --) { Result[ResultEnd++] = TempResult[DivisorEnd] + '0'; } Result[ResultEnd]= '\0'; return Result;}int main(){ char InputDividend[MaxSize] = "5409656775097850895687056798068970934546546575676768678435435345"; char InputDivisor[MaxSize] = "1"; char *Result = TwoBigNumDivision(InputDividend,InputDivisor); puts(Result); return 0;}

 

转载于:https://www.cnblogs.com/Asurudo/p/9427295.html

你可能感兴趣的文章
CListCtrl控件
查看>>
debian8.2 + postgresql 9.1 + apt-get 的一些路径
查看>>
mySql执行效率分析
查看>>
矩阵乘法的Strassen算法
查看>>
[Think In Java]基础拾遗4 - 并发
查看>>
angular监听
查看>>
架构-缓存
查看>>
iOS开发中遇到的问题:Multiple commands produce '^^^^^
查看>>
PE2 Even Fibonacci numbers(最大菲波那列偶数)
查看>>
【Socket】linux无连接编程技术
查看>>
客户购买我方产品的时候,是将款打到哪里?
查看>>
集群时间同步
查看>>
ORACLE触发器详解
查看>>
ansible-1 的安装
查看>>
gulp介绍
查看>>
关于asp.net中gridview的问题,关于footer,16aspx上下的英语交流网程序,管理员的添加和修改有问题...
查看>>
Codeforces 242 E. XOR on Segment
查看>>
siblings() next() nextAll() nextUntil() prev() prevAll() prevUntil() 在 DOM 树中水平遍历
查看>>
Nodejs技巧
查看>>
trigger事件就是继承某一个类的事件.
查看>>