随笔-68  评论-10  文章-0  trackbacks-0
#include<iostream>
#include
<string>
using namespace std;

string BigIntegerDiv(const string &str1,const string &str2)  //  str1/str2
{
    
int i;
    
int length,len1,len2,len,index;
    
bool flag;
    
    len1
=str1.length();
    len2
=str2.length();
    length
=len1-len2;
    
if(length<0return "0";
    
    
int *num1=new int[len1+1];
    
int *num2=new int[len2+1];
    
int *quotient=new int[length+2];
    
    memset(num1,
0,sizeof(int)*(len1+1));
    memset(num2,
0,sizeof(int)*(len2+1));
    memset(quotient,
0,sizeof(int)*(length+2));
    
    
for(i=len1-1,index=0;i>=0;i--)
    
{
        num1[index
++]=str1[i]-'0';
    }

    
for(i=len2-1,index=0;i>=0;i--)
    
{
        num2[index
++]=str2[i]-'0';
    }

    
    
while(true)
    
{
        
for(i=len1-1;i>=0;i--)
        
{
            
if(num1[i])
            
{
                len1
=i+1;
                
break;
            }

            
if(i==0) len1=0;
        }

        
        len
=len1-len2;
        
if(len<0break;
        
        flag
=false;
        index
=0;
        
        
for(i=len1-1;i>=len;i--)
        
{
            
if(num1[i]==num2[i-len]) continue;
            
else if(num1[i]<num2[i-len])
            
{
                flag
=true;
                
break;
            }

            
else
            
{
                
break;
            }

        }

        
        
if(flag) --len;
        
if(len<0break;
        
        
while(++index)
        
{
            flag
=false;
            
for(i=len1-1;i>=len;i--)
            
{
                
if(num1[i]==num2[i-len]) continue;
                
else if(num1[i]<num2[i-len])
                
{
                    flag
=true;
                    
break;
                }

                
else
                
{
                    
break;
                }

            }

            
if(flag)
            
{
                
--index;
                
break;
            }


            
for(i=len;i<len1;i++)
            
{
                num1[i]
-=num2[i-len];
                
if(num1[i]<0)
                
{
                    num1[i]
+=10;
                    
--num1[i+1];
                }

            }

        }

        
if(index==0break;
        
else quotient[len]=index;
    }


    
string ans;
    flag
=false;
    
for(i=length;i>=0;i--)
    
{
        
if(flag||quotient[i])
        
{
            flag
=true;
            ans
+='0'+quotient[i];
        }

    }

    
if(!flag) ans="0";

    delete [] num1;
    delete [] num2;
    delete [] quotient;

    
return ans;
}


int main()
{
    
string str,str1,str2;
    
while(cin>>str1>>str2)
    
{
        str
=BigIntegerDiv(str1,str2);
        cout
<<str<<endl;
    }

    
return 0;
}

posted on 2011-07-31 15:22 wuxu 阅读(993) 评论(0)  编辑 收藏 引用 所属分类: 高精度

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理