标签:namespace name 运算 cstring main out tar ike com
高精度运算,是指参与运算的数(加数,减数,因子……)范围大大超出了标准数据类型(整型,实型)能表示的范围的运算。例如,求两个20000位的数的和。这时,就要用到高精度算法了。
1、高精度加法
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
struct node
{
int len,a[510];
node()
{
len=0;
memset(a,0,sizeof(a));
}
};
int mymax(int x,int y)
{
return x>y?x:y;
}
node jiafa(node n1,node n2)
{
node n0;
n0.len=mymax(n1.len,n2.len);
for(int i=1;i<=n0.len;i++)
{
n0.a[i]=n1.a[i]+n2.a[i];
}//先每个位相加
for(int i=1;i<=n0.len;i++)
{
n0.a[i+1]+=n0.a[i]/10;
n0.a[i]%=10;
}//进位
int i=n0.len;
while(n0.a[i+1]>0)
{
i++;
n0.a[i+1]+=n0.a[i]/10;
n0.a[i]%=10;
}
while((n0.a[i]==0)&&(i>1))
i--;
n0.len=i;
return n0;
}//判断位数
int main()
{
char st[511];
node n0,n1,n2;
scanf("%s",st+1);
n1.len=strlen(st+1);
for(int i=1;i<=n1.len;i++)
n1.a[n1.len-i+1]=st[i]-‘0‘;
scanf("%s",st+1);
n2.len=strlen(st+1);
for(int i=1;i<=n2.len;i++)
n2.a[n2.len-i+1]=st[i]-‘0‘;
n0=jiafa(n1,n2);
for(int i=n0.len;i>=1;i--)
printf("%d",n0.a[i]);
cout<<endl;
return 0;
}
2、高精度减法
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
struct node
{
int len,a[510];
node()
{
len=0;
memset(a,0,sizeof(a));
}
};
int mymax(int x,int y)
{
return x>y?x:y;
}
int bijiao(node n1,node n2)
{
if(n1.len>n2.len) return 1;
if(n1.len<n2.len) return -1;
for(int i=n1.len;i>=1;i--)
{
if(n1.a[i]>n2.a[i]) return 1;
if(n1.a[i]<n2.a[i]) return -1;
}
return 0;
}
node jianfa(node n1,node n2)
{
node n0;
n0.len=n1.len;
for(int i=1;i<=n0.len;i++)
n0.a[i]=n1.a[i]-n2.a[i];
for(int i=1;i<=n0.len;i++)
{
if(n0.a[i]<0)
{
n0.a[i+1]--;
n0.a[i]+=10;
}
}
int i=n0.len;
while(n0.a[i+1]>0)
{
i++;
n0.a[i+1]=n0.a[i]/10;
n0.a[i]%=10;
}
while((n0.a[i]==0)&&(i>1)) i--;
n0.len=i;
return n0;
}
int main()
{
char st[511];
node n0,n1,n2;
scanf("%s",st+1);
n1.len=strlen(st+1);
for(int i=1;i<=n1.len;i++)
n1.a[n1.len-i+1]=st[i]-‘0‘;
scanf("%s",st+1);
n2.len=strlen(st+1);
for(int i=1;i<=n2.len;i++)
n2.a[n2.len-i+1]=st[i]-‘0‘;
int tt=bijiao(n1,n2);//判断结果是否为负
if(tt>=0) n0=jianfa(n1,n2);
else n0=jianfa(n2,n1);//用较大的减
if(tt<0)
{
cout<<"-";//为负则输出“-”
for(int i=n0.len;i>=1;i--)
printf("%d",n0.a[i]);
}
else
{
for(int i=n0.len;i>=1;i--)
printf("%d",n0.a[i]);
}
}
3、高精度乘法
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
struct node
{
int len,a[510];
node()
{
len=0;
memset(a,0,sizeof(a));
}
};
node chengfa(node n1,node n2)
{
node n0;
n0.len=n1.len+n2.len-1;
for(int i=1;i<=n1.len;i++)
{
for(int j=1;j<=n2.len;j++)
{
n0.a[i+j-1]+=n1.a[j]*n2.a[j];
}
}//累乘
for(int i=1;i<=n0.len;i++)
{
n0.a[i+1]+=n0.a[i]/10;
n0.a[i]%=10;
}//进位
int i=n0.len;
while(n0.a[i+1]>0)
{
i++;
n0.a[i+1]=n0.a[i]/10;
n0.a[i]%=10;
}
while((n0.a[i]==0)&&(i>1)) i--;
n0.len=i;
return n0;
} //判断位数
int main()
{
char st[511];
node n0,n1,n2;
scanf("%s",st+1);
n1.len=strlen(st+1);
for(int i=1;i<=n1.len;i++)
n1.a[n1.len-i+1]=st[i]-‘0‘;
scanf("%s",st+1);
n2.len=strlen(st+1);
for(int i=1;i<=n2.len;i++)
n2.a[n2.len-i+1]=st[i]-‘0‘;
n0=chengfa(n1,n2);
for(int i=n0.len;i>=1;i--) cout<<n0.a[i];
cout<<endl;
return 0;
}
标签:namespace name 运算 cstring main out tar ike com
原文地址:https://www.cnblogs.com/zhaoxuelin/p/12457951.html