#include<bits/stdc++.h>
using namespace std;
#define x first
#define y second
typedef pair<int,int>PII;
typedef pair<double,double>PDD;
typedef long long LL;
typedef unsigned long long ull;
#define N 10000
typedef struct dashu
{
int len;
int arr[N];
dashu()
{
len=0;
memset(arr,0,sizeof(arr));
}
}daShu;
char aa[N],bb[N];
daShu change(char str[]);
void printDaShu(daShu s);
daShu add(daShu a,daShu b);
daShu jian(daShu a,daShu b);
daShu cheng(daShu a,int b);
daShu chu(daShu a,int b,int &down);
daShu jieCheng(int n);
daShu DaChengDa(daShu a,daShu b);
daShu DaChuDa(daShu a,daShu b,daShu &Yu);
int main()
{
scanf("%s%s",aa,bb);
daShu a=change(aa);
daShu b=change(bb);
//daShu c=add(a,b);
//daShu c=jian(a,b);
//daShu c=cheng(a,3);
//daShu c=chu(a,3,down);
//daShu c=jieCheng(999);
//daShu c=DaChengDa(a,b);
int down;
daShu Yu;
daShu c=DaChuDa(a,b,Yu);
printDaShu(c);
printf("\n");
printDaShu(Yu);
return 0;
}
//字符串转数组
daShu change(char str[])
{
daShu s;
s.len=strlen(str);
for(int i=0;i<s.len;i++)
{
s.arr[i]=str[s.len-i-1]-'0';
}
return s;
}
//打印大数
void printDaShu(daShu s)
{
for(int i=s.len-1;i>-1;i--)
{
printf("%d",s.arr[i]);
}
}
//大数相加
daShu add(daShu a,daShu b)
{
daShu s;
int up=0,temp;
for(int i=0;i<a.len||i<b.len;i++)
{
temp=a.arr[i]+b.arr[i]+up;
s.arr[i]=temp%10;
up=temp/10;
s.len++;
}
if(up!=0)
{
s.arr[s.len++]=up;
}
return s;
}
//大数相减
daShu jian(daShu a,daShu b)
{
daShu s;
for(int i=0;i<a.len||i<b.len;i++)
{
if(a.arr[i]<b.arr[i])
{
a.arr[i+1]--;
a.arr[i]+=10;
}
s.arr[i]=a.arr[i]-b.arr[i];
s.len++;
}
while(s.len>0&&s.arr[s.len-1]==0)
{
s.len--;
}
return s;
}
//大数乘int
daShu cheng(daShu a,int b)
{
daShu s;
int up=0;
for(int i=0;i<a.len;i++)
{
int temp=a.arr[i]*b+up;
s.arr[i]=temp%10;
up=temp/10;
s.len++;
}
while(up)
{
s.arr[s.len++]=up%10;
up/=10;
}
return s;
}
//大数除int
daShu chu(daShu a,int b,int &down)
{
daShu s;
down=0;
s.len=a.len;
for(int i=a.len-1;i>-1;i--)
{
down=down*10+a.arr[i];
if(down<b)
{
s.arr[i]=0;
}
else
{
s.arr[i]=down/b;
down=down%b;
}
}
while(s.len>0&&s.arr[s.len-1]==0)
{
s.len--;
}
return s;
}
//大数阶乘
daShu jieCheng(int n)
{
daShu s;
s.arr[0]=1;
s.len=1;
for(int i=1;i<=n;i++)
{
s=cheng(s,i);
}
return s;
}
//大数相乘
daShu DaChengDa(daShu a,daShu b)
{
daShu s;
if(a.len+b.len>N||a.arr[0]==0||b.arr[0]==0)
{
s.len=1;
return s;
}
s.len=a.len+b.len;
for(int i=0;i<b.len;i++)
{
for(int j=0;j<a.len;j++)
{
s.arr[i+j]+=a.arr[j]*b.arr[i];
}
}
for(int i=0;i<s.len;i++)
{
if(s.arr[i]>10)
{
s.arr[i+1]+=s.arr[i]/10;
s.arr[i]%=10;
}
}
while(s.len>0&&s.arr[s.len-1]==0)
{
s.len--;
}
return s;
}
//大数相除
daShu DaChuDa(daShu a,daShu b,daShu &Yu)
{
int bLen=b.len;
int aLen=a.len;
daShu c,s;
for(int k=aLen;k>=bLen;k--)
{
memset(c.arr,0,sizeof(c.arr));
for(int i=k-1,j=bLen-1;j>=0;i--,j--)
{
c.arr[i]=b.arr[j];
}
c.len=k;
int n=k-bLen;
int cnt=0;
while((a.len==c.len&&c.arr[c.len-1]<=a.arr[a.len-1])||a.len>c.len)
{
a=jian(a,c);
cnt++;
}
s.arr[n]=cnt;
}
Yu=a;
if(Yu.len==0)
{
Yu.len=1;
Yu.arr[0]=0;
}
s.len=aLen;
while(s.len>0&&s.arr[s.len-1]==0)
{
s.len--;
}
return s;
}
大数运算(高精度)
最新推荐文章于 2024-04-23 18:09:35 发布