标程_高精度运算

标程_高精度运算

高精加高精

#include<iostream>
#include<cstdio>
#include<string> 
#include<cstring> 
using namespace std;
string s1,s2;
int a[1010],b[1010],c[1010],len1,len2,alen,blen,clen,llen,s;
void in_()
{
    
    
	int i,j;
	memset(a,0,sizeof(a));
	memset(b,0,sizeof(b));
	memset(c,0,sizeof(c));
	cin>>s1;
	cin>>s2;
	len1=s1.size();
	len2=s2.size();
	llen=max(len1,len2);
	for(i=llen,j=len1-1;j>=0;i--,j--)
	{
    
    
		a[i]=s1[j]-'0';
	}
	for(i=llen,j=len2-1;j>=0;i--,j--)
	{
    
    
		b[i]=s2[j]-'0';
	}
	return;
}
void plus_()
{
    
    
	int i;
	s=0;
	for(i=llen;i>=1;i--)
	{
    
    
		c[i]=a[i]+b[i]+s;
		s=c[i]/10;
		c[i]=c[i]%10;
	}
	c[0]=s;
	return;
}
void out_()
{
    
    
	int i;
	if (s>0)
		printf("%d",s);
	for(i=1;i<=llen;i++)
		printf("%d",c[i]);
	return;
}
int main()
{
    
    
	in_();
	plus_();
	out_();
	return 0; 
}

高精减高精

#include<iostream>
#include<cstdio>
#include<string> 
#include<cstring> 
using namespace std;
char s1[1000],s2[1000];
bool t;
long long a[1010],b[1010],c[1010],len1,len2,n;
void in_()
{
    
    
	int i,j;
	memset(a,0,sizeof(a));
	memset(b,0,sizeof(b));
	memset(c,0,sizeof(c));
	scanf("%s",&s1);
	scanf("%s",&s2);
	len1=strlen(s1);
	len2=strlen(s2);
	for(i=1001-len1;i<=1000;i++)
		a[i]=s1[i+len1-1001]-'0';
	for(i=1001-len2;i<=1000;i++)
		b[i]=s2[i+len2-1001]-'0';
	return;
}
bool bj()
{
    
    
	if (len1<len2||(len1==len2&&strcmp(s1,s2)==-1))
		return 1;
	return 0;
}
bool bj_()
{
    
    
	if (len1==len2&&strcmp(s1,s2)==0)
		return 1;
	return 0;
}
void jian_()
{
    
    
	int i;
	for(i=1000;i>=1;i--)
	{
    
    
		if (a[i]<b[i])
		{
    
    
			a[i-1]--;
			a[i]+=10;
		}
		c[i]=a[i]-b[i];
	}
	return;
}
void out_()
{
    
    
	int i;
	if (t)
		printf("-");
	for(i=1;c[i]==0&&i<=1000;i++);
	for(;i<=1000;i++)
		printf("%d",c[i]);
	return;
}
int main()
{
    
    
	int i;
	in_();
	if (bj_())
	{
    
    
		printf("0");
		return 0;
	}
	t=bj();
	if (t)
	{
    
    
		memcpy(c,a,sizeof(a));
		memcpy(a,b,sizeof(b));
		memcpy(b,c,sizeof(a));
		memset(c,0,sizeof(c));
	}
	jian_();
	out_();
	return 0; 
}

高精乘单精

#include<iostream>
#include<cstdio>
#include<string> 
#include<cstring> 
using namespace std;
string s;
long long a[1010],b,c[1010],len,w;
void in_()
{
    
    
	int i,j;
	memset(a,0,sizeof(a));
	memset(c,0,sizeof(c));
	cin>>s;
	cin>>b;
	len=s.size();
	for(i=len;i>0;i--)
	{
    
    
		a[i]=s[i-1]-'0';
	}
	return;
}
void plus_()
{
    
    
	int i;
	w=0;
	for(i=len;i>=1;i--)
	{
    
    
		c[i]=a[i]*b+w;
		w=c[i]/10;
		c[i]=c[i]%10;
	}
	c[0]=w;
	return;
}
void out_()
{
    
    
	int i;
	if (w>0)
		printf("%d",w);
	for(i=1;c[i]==0&&i<=len;i++);
	if (i>len)
	{
    
    
		printf("0");
		return;	
	}
	for(;i<=len;i++)
		printf("%d",c[i]);
	return;
}
int main()
{
    
    
	in_();
	plus_();
	out_();
	return 0; 
}

高精乘高精

#include<iostream>
#include<cstdio>
#include<string> 
#include<cstring> 
using namespace std;
char s1[1010],s2[1010];
long long a[1010],b[1010],c[1010],len1,len2,llen,w;
void in_()
{
    
    
	int i,j;
	memset(a,0,sizeof(a));
	memset(b,0,sizeof(b));
	memset(c,0,sizeof(c));
	scanf("%s",&s1);
	scanf("%s",&s2);
	len1=strlen(s1);
	len2=strlen(s2);
	llen=max(len1,len2);
	for(i=1,j=len1-1;j>=0;i++,j--)
	{
    
    
		a[i]=s1[j]-'0';
	}
	for(i=1,j=len2-1;j>=0;i++,j--)
	{
    
    
		b[i]=s2[j]-'0';
	}
	return;
}
void cheng_()
{
    
    
	int i,j;
	for(i=1;i<=len1;i++)
	{
    
    
		for(j=1;j<=len2;j++) 
		{
    
    
			c[i+j-1]+=a[i]*b[j];
			c[i+j]+=c[i+j-1]/10;
			c[i+j-1]=c[i+j-1]%10;
		}
	}
	return;
}
void out_()
{
    
    
	int i;
	for(i=1000;c[i]==0&&i>=1;i--);
	if (i==0)
	{
    
    
		printf("0");
		return;	
	}
	for(;i>=1;i--)
		printf("%lld",c[i]);
	return;
}
int main()
{
    
    
	in_();
	cheng_();
	out_();
	return 0; 
}

高精除单精

#include<iostream>
#include<cstdio>
#include<string> 
#include<cstring> 
using namespace std;
char s[1010];
long long a[1010],c[1010],b,len,g;
void in_()
{
    
    
	int i,j;
	memset(a,0,sizeof(a));
	memset(c,0,sizeof(c));
	scanf("%s",&s);
	scanf("%lld",&b);
	len=strlen(s);
	for(i=len;i>0;i--)
	{
    
    
		a[i]=s[i-1]-'0';
	}
	return;
}
void chu_()
{
    
    
	int i;
	g=0;
	for(i=1;i<=len;i++)
	{
    
    
		c[i]=(g*10+a[i])/b;
		g=(g*10+a[i])%b;
	}
	return;
}
void out_()
{
    
    
	int i;
	for(i=1;c[i]==0&&i<=len;i++);
	if (i>len)
		printf("0");
	else
	{
    
    
		for(;i<=len;i++)
			printf("%lld",c[i]);	
	}
	printf("...%lld",g);
	return;
}
int main()
{
    
    
	in_();
	chu_();
	out_();
	return 0; 
}

高精除高精_减

#include<iostream>
#include<cstdio>
#include<string> 
#include<cstring> 
using namespace std;
char s1[1010],s2[1010];
long long a[1010],b[1010],c[1010],len1,len2,llen;
void in_()
{
    
    
	int i,j;
	memset(a,0,sizeof(a));
	memset(b,0,sizeof(a));
	memset(c,0,sizeof(c));
	scanf("%s",&s1);
	scanf("%s",&s2);
	len1=strlen(s1);
	len2=strlen(s2);
	llen=max(len1,len2);
	for(i=llen,j=len1-1;j>=0;i--,j--)
	{
    
    
		a[i]=s1[j]-'0';
	}
	for(i=llen,j=len2-1;j>=0;i--,j--)
	{
    
    
		b[i]=s2[j]-'0';
	}
	return;
}
bool cmp()
{
    
    
	int i,l1,l2;
	for(i=1;a[i]==0;i++);
	l1=llen-i+1;
	for(i=1;b[i]==0;i++);
	l2=llen-i+1;
	if (l1<l2)
		return 0;
	if (l1>l2)
		return 1;
	for(i=l1;i<=llen;i++)
	{
    
    
		if (a[i]>b[i])
			return 1;
		if (a[i]<b[i])
			return 0;
	}
	return 1;
}
void add()
{
    
    
	int i;
	c[llen]++;
	for(i=llen;c[i]>=10;i--)
	{
    
    
		c[i-1]++;
		c[i]=c[i]%10;
	}
	return;
} 
void sub()
{
    
    
	int i;
	for(i=llen;i>=1;i--)
	{
    
    
		if (a[i]<b[i])
		{
    
    
			a[i-1]--;
			a[i]+=10;
		}
		a[i]=a[i]-b[i];
	}
	return;
}
void out_()
{
    
    
	int i;
	for(i=1;c[i]==0&&i<=llen;i++);
	if (i>llen)
		printf("0");
	else
	{
    
    
		for(;i<=llen;i++)
			printf("%lld",c[i]);	
	}
	printf("...");
	for(i=1;a[i]==0&&i<=llen;i++);
	if (i>llen)
		printf("0");
	else
	{
    
    
		for(;i<=llen;i++)
			printf("%lld",a[i]);	
	}
	return;
}
int main()
{
    
    
	in_();
	while(cmp())
	{
    
    
		sub();
		add();
	}
	out_();
	return 0; 
}

高精除高精_移减

#include<iostream>
#include<cstdio>
#include<string> 
#include<cstring> 
using namespace std;
char s1[1010],s2[1010];
long long a[1010],b[1010],c[1010],len1,len2,llen,w,ww;
void input()
{
    
    
	int i,j;
	memset(a,0,sizeof(a));
	memset(b,0,sizeof(b));
	memset(c,0,sizeof(c));
	scanf("%s",&s1);
	scanf("%s",&s2);
	len1=strlen(s1);
	len2=strlen(s2);
	llen=max(len1,len2);
	for(i=llen,j=len1-1;j>=0;i--,j--)
	{
    
    
		a[i]=s1[j]-'0';
	}
	for(i=llen,j=len2-1;j>=0;i--,j--)
	{
    
    
		b[i]=s2[j]-'0';
	}
	return;
}
bool cmp()
{
    
    
	int i,l1,l2;
	for(i=0;a[i]==0&&i<=llen;i++);
	l1=llen-i+1;
	for(i=0;b[i]==0&&i<=llen;i++);
	l2=llen-i+1;
	if (l1<l2)
		return 0;
	if (l1>l2)
		return 1;
	for(i=l1;i<=llen;i++)
	{
    
    
		if (a[i]>b[i])
			return 1;
		if (a[i]<b[i])
			return 0;
	}
	return 1;
}
void ywl()
{
    
    
	int i;
	for(i=0;b[i]==0&&i<=ww;i++);
	for(;i<=ww;i++)
	{
    
    
		b[i-1]=b[i];
	}
	b[ww]=0;
	ww--;
	w++;
	return;
}
void ywr()
{
    
    
	int t,i;
	for(i=0;b[i]==0&&i<=ww;i++);
	t=i;
	for(i=ww+1;i>t;i--)
	{
    
    
		b[i]=b[i-1];
	}
	b[t]=0;
	ww++;
	w--;
	return;
}
void add()
{
    
    
	c[w]++;
	return;
} 
void sub()
{
    
    
	int i;
	for(i=llen;i>=1;i--)
	{
    
    
		if (a[i]<b[i])
		{
    
    
			a[i-1]--;
			a[i]+=10;
		}
		a[i]-=b[i];
	}
	return;
}
void output()
{
    
    
	int i;
	for(i=1000;c[i]==0&&i>=0;i--);
	for(;i>=0;i--)
		printf("%lld",c[i]);
	printf("...");
	for(i=1;a[i]==0&&i<=llen;i++);
	if (i>llen)
		printf("0");
	else
	{
    
    
		for(;i<=llen;i++)
			printf("%lld",a[i]);	
	}
	return;
}
int main()
{
    
    
	int i;
	input();
	if (!(cmp()))
	{
    
    
		printf("0...");
		for(i=1;a[i]==0&&i<=llen;i++);
		if (i>llen)
			printf("0");
		else
		{
    
    
			for(;i<=llen;i++)
				printf("%lld",a[i]);	
		}
		return 0;
	}
	ww=llen;
	w=0;
	while(cmp())
	{
    
    
		ywl();
	}
	ywr();
	while(w>=0)
	{
    
    
		while(cmp())
		{
    
    
			sub();
			add();
		}
		ywr();
	}
	output();
	return 0; 
}

猜你喜欢

转载自blog.csdn.net/weixin_46975572/article/details/115029617