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

string BigIntegerMod(const string &str1,const string &str2) // str1%str2
{
    
int i;
    
int len,len1,len2,index;
    
bool flag;

    len1
=str1.length();
    len2
=str2.length();
    len
=len1-len2;
    
if(len<0return str1;

    
int *num1=new int[len1+2];
    
int *num2=new int[len2+2];

    memset(num1,
0,sizeof(int)*(len1+2));
    memset(num2,
0,sizeof(int)*(len2+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;
    }


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

    }

    
if(!flag) ans="0";

    delete [] num1;
    delete [] num2;

    
return ans;
}


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

    
return 0;
}

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

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