#include<iostream>
#include<string>
using namespace std;
![](/Images/OutliningIndicators/None.gif)
string BigIntegerMod(const string &str1,const string &str2) // str1%str2
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
int i;
int len,len1,len2,index;
bool flag;
![](/Images/OutliningIndicators/InBlock.gif)
len1=str1.length();
len2=str2.length();
len=len1-len2;
if(len<0) return str1;
![](/Images/OutliningIndicators/InBlock.gif)
int *num1=new int[len1+2];
int *num2=new int[len2+2];
![](/Images/OutliningIndicators/InBlock.gif)
memset(num1,0,sizeof(int)*(len1+2));
memset(num2,0,sizeof(int)*(len2+2));
![](/Images/OutliningIndicators/InBlock.gif)
for(i=len1-1,index=0;i>=0;--i)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
num1[index++]=str1[i]-'0';
}
for(i=len2-1,index=0;i>=0;--i)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
num2[index++]=str2[i]-'0';
}
![](/Images/OutliningIndicators/InBlock.gif)
while(true)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
for(i=len1-1;i>=0;i--)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(num1[i])
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
len1=i+1;
break;
}
if(i==0) len1=0;
}
![](/Images/OutliningIndicators/InBlock.gif)
len=len1-len2;
if(len<0) break;
![](/Images/OutliningIndicators/InBlock.gif)
flag=false;
index=0;
![](/Images/OutliningIndicators/InBlock.gif)
for(i=len1-1;i>=len;i--)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(num1[i]==num2[i-len]) continue;
else if(num1[i]<num2[i-len])
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
flag=true;
break;
}
else break;
}
![](/Images/OutliningIndicators/InBlock.gif)
if(flag) --len;
if(len<0) break;
![](/Images/OutliningIndicators/InBlock.gif)
while(++index)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
flag=false;
for(i=len1-1;i>=len;i--)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(num1[i]==num2[i-len]) continue;
else if(num1[i]<num2[i-len])
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
flag=true;
break;
}
else break;
}
if(flag)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
--index;
break;
}
![](/Images/OutliningIndicators/InBlock.gif)
for(i=len;i<len1;i++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
num1[i]-=num2[i-len];
if(num1[i]<0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
num1[i]+=10;
--num1[i+1];
}
}
}
![](/Images/OutliningIndicators/InBlock.gif)
if(index==0) break;
}
![](/Images/OutliningIndicators/InBlock.gif)
string ans;
flag=false;
for(i=len1;i>=0;--i)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(flag||num1[i])
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
flag=true;
ans+='0'+num1[i];
}
}
if(!flag) ans="0";
![](/Images/OutliningIndicators/InBlock.gif)
delete [] num1;
delete [] num2;
![](/Images/OutliningIndicators/InBlock.gif)
return ans;
}
![](/Images/OutliningIndicators/None.gif)
int main()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
string str,str1,str2;
while(cin>>str1>>str2)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
str=BigIntegerMod(str1,str2);
cout<<str<<endl;
}
return 0;
}
![](/Images/OutliningIndicators/None.gif)
posted on 2011-07-31 15:24
wuxu 阅读(491)
评论(0) 编辑 收藏 引用 所属分类:
高精度