当数的位数非常大而常规的数据类型不足以存储的时候,可以考虑使用大数。即用一个长的数组来存储相应的位数,如int a[100],a的每一位存储一位数字或几位数字,根据常规的加减乘除运算对数组进行操作。使用string类型对数组进行操作,虽然比用整型简便,但可 能增加了运行时间。pku1001:Exponentiation 就用到了大数的乘法
如下是一个使用string类型进行大数加减乘除的例子:
#include<iostream>
#include<string>
#include<stdio.h>
#include<algorithm>
using namespace std;
bool comp(string num1,string num2)
{
int leng=num1.length(),i;
for(i=0;i<leng;i++){if(num1[i]!='0')break;}
num1=num1.substr(i,leng);
if(num1.length()==0)num1="0";
leng=num2.length();
for(i=0;i<leng;i++){if(num2[i]!='0')break;}
num2=num2.substr(i,leng);
if(num2.length()==0)num2="0";
if(num1.length()>num2.length())return true;
else if(num1.length()==num2.length())
{
if(num1>=num2)return true;
else return false;
}
else return false;
}
string _plus(string num1,string num2)
{
string result="";
reverse(num1.begin(),num1.end());
reverse(num2.begin(),num2.end());
int i;
for(i=0;i<num1.length()&&i<num2.length();i++)
{
char c=(char)(num1[i]+num2[i]-48);
result=result+c;
}
while(i<num1.length()){result=result+num1[i];i++;}
while(i<num2.length()){result=result+num2[i];i++;}
int jinwei=0;
for(i=0;i<result.length();i++)
{
int zhi=result[i]-48+jinwei;
result[i]=(char)(zhi%10+48);
jinwei=zhi/10;
}
if(jinwei!=0)result=result+(char)(jinwei+48);
for(i=result.length()-1;i>=0;i--){if(result[i]!='0')break;}
result=result.substr(0,i+1);
reverse(result.begin(),result.end());
if(result.length()==0)result="0";
return result;
}
string _minus(string num1,string num2)
{
if(comp(num2,num1)){string ss=num1;num1=num2;num2=ss;}
reverse(num1.begin(),num1.end());
reverse(num2.begin(),num2.end());
string result="";
int i;
for(i=0;i<num1.length()&&i<num2.length();i++)
{
char c=num1[i]-num2[i]+48;
result=result+c;
}
if(i<num1.length())for(;i<num1.length();i++)result=result+num1[i];
int jiewei=0;
for(i=0;i<result.length();i++)
{
int zhi=result[i]-48+jiewei;
if(zhi<0){zhi=zhi+10;jiewei=-1;}
else jiewei=0;
result[i]=(char)(zhi+48);
}
for(i=result.length()-1;i>=0;i--){if(result[i]!='0')break;}
result=result.substr(0,i+1);
reverse(result.begin(),result.end());
if(result.length()==0)result="0";
return result;
}
string _mult(string num1,string num2)
{
reverse(num1.begin(),num1.end());
reverse(num2.begin(),num2.end());
string result="";
int i,j,k,re_int[150];
memset(re_int,0,sizeof(re_int));
for(i=0;i<num1.length();i++)
for(j=0;j<num2.length();j++)
{
re_int[i+j]+=((num1[i]-48)*(num2[j]-48));
}
//for(i=num1.length()+num2.length()-1;i>=0;i--)cout<<re_int[i]<<' ';cout<<endl;
int jinwei=0;
for(i=0;i<num1.length()+num2.length();i++)
{
int zhi=re_int[i]+jinwei;
re_int[i]=zhi%10;
jinwei=zhi/10;
}
for(i=num1.length()+num2.length()-1;i>=0;i--)if(re_int[i]!=0)break;
for(;i>=0;i--){result=result+(char)(re_int[i]+48);}
if(result=="")result="0";
return result;
}
string _divid(string num1,string num2)
{
if(comp(num2,num1)){string ss=num1;num1=num2;num2=ss;}
string result="",r1;
int i,j,k;
int length1=num1.length(),length2=num2.length(),pos;
r1=num1.substr(0,length2);
pos=length2-1;
while(pos<length1)
{
int digit=0;
while(comp(r1,num2)){digit++;r1=_minus(r1,num2);}
result=result+(char)(digit+48);
pos++;
if(pos<length1)r1+=num1[pos];
}
k=result.length();
for(i=0;i<k;i++)if(result[i]!='0')break;
result=result.substr(i,k);
return result;
}
int main(int argc, char* argv[])
{
string num1,num2,result;
int a,b,c,i,j,k,m,n;
while(cin>>num1>>num2)
{
result="";
result=_plus(num1,num2);
cout<<result<<endl;
result=_minus(num1,num2);
cout<<result<<endl;
result=_mult(num1,num2);
cout<<result<<endl;
result=_divid(num1,num2);
cout<<result<<endl;
}
return 0;
}
如下是一个使用string类型进行大数加减乘除的例子:
#include<iostream>
#include<string>
#include<stdio.h>
#include<algorithm>
using namespace std;
bool comp(string num1,string num2)
{
int leng=num1.length(),i;
for(i=0;i<leng;i++){if(num1[i]!='0')break;}
num1=num1.substr(i,leng);
if(num1.length()==0)num1="0";
leng=num2.length();
for(i=0;i<leng;i++){if(num2[i]!='0')break;}
num2=num2.substr(i,leng);
if(num2.length()==0)num2="0";
if(num1.length()>num2.length())return true;
else if(num1.length()==num2.length())
{
if(num1>=num2)return true;
else return false;
}
else return false;
}
string _plus(string num1,string num2)
{
string result="";
reverse(num1.begin(),num1.end());
reverse(num2.begin(),num2.end());
int i;
for(i=0;i<num1.length()&&i<num2.length();i++)
{
char c=(char)(num1[i]+num2[i]-48);
result=result+c;
}
while(i<num1.length()){result=result+num1[i];i++;}
while(i<num2.length()){result=result+num2[i];i++;}
int jinwei=0;
for(i=0;i<result.length();i++)
{
int zhi=result[i]-48+jinwei;
result[i]=(char)(zhi%10+48);
jinwei=zhi/10;
}
if(jinwei!=0)result=result+(char)(jinwei+48);
for(i=result.length()-1;i>=0;i--){if(result[i]!='0')break;}
result=result.substr(0,i+1);
reverse(result.begin(),result.end());
if(result.length()==0)result="0";
return result;
}
string _minus(string num1,string num2)
{
if(comp(num2,num1)){string ss=num1;num1=num2;num2=ss;}
reverse(num1.begin(),num1.end());
reverse(num2.begin(),num2.end());
string result="";
int i;
for(i=0;i<num1.length()&&i<num2.length();i++)
{
char c=num1[i]-num2[i]+48;
result=result+c;
}
if(i<num1.length())for(;i<num1.length();i++)result=result+num1[i];
int jiewei=0;
for(i=0;i<result.length();i++)
{
int zhi=result[i]-48+jiewei;
if(zhi<0){zhi=zhi+10;jiewei=-1;}
else jiewei=0;
result[i]=(char)(zhi+48);
}
for(i=result.length()-1;i>=0;i--){if(result[i]!='0')break;}
result=result.substr(0,i+1);
reverse(result.begin(),result.end());
if(result.length()==0)result="0";
return result;
}
string _mult(string num1,string num2)
{
reverse(num1.begin(),num1.end());
reverse(num2.begin(),num2.end());
string result="";
int i,j,k,re_int[150];
memset(re_int,0,sizeof(re_int));
for(i=0;i<num1.length();i++)
for(j=0;j<num2.length();j++)
{
re_int[i+j]+=((num1[i]-48)*(num2[j]-48));
}
//for(i=num1.length()+num2.length()-1;i>=0;i--)cout<<re_int[i]<<' ';cout<<endl;
int jinwei=0;
for(i=0;i<num1.length()+num2.length();i++)
{
int zhi=re_int[i]+jinwei;
re_int[i]=zhi%10;
jinwei=zhi/10;
}
for(i=num1.length()+num2.length()-1;i>=0;i--)if(re_int[i]!=0)break;
for(;i>=0;i--){result=result+(char)(re_int[i]+48);}
if(result=="")result="0";
return result;
}
string _divid(string num1,string num2)
{
if(comp(num2,num1)){string ss=num1;num1=num2;num2=ss;}
string result="",r1;
int i,j,k;
int length1=num1.length(),length2=num2.length(),pos;
r1=num1.substr(0,length2);
pos=length2-1;
while(pos<length1)
{
int digit=0;
while(comp(r1,num2)){digit++;r1=_minus(r1,num2);}
result=result+(char)(digit+48);
pos++;
if(pos<length1)r1+=num1[pos];
}
k=result.length();
for(i=0;i<k;i++)if(result[i]!='0')break;
result=result.substr(i,k);
return result;
}
int main(int argc, char* argv[])
{
string num1,num2,result;
int a,b,c,i,j,k,m,n;
while(cin>>num1>>num2)
{
result="";
result=_plus(num1,num2);
cout<<result<<endl;
result=_minus(num1,num2);
cout<<result<<endl;
result=_mult(num1,num2);
cout<<result<<endl;
result=_divid(num1,num2);
cout<<result<<endl;
}
return 0;
}