高精度计算

<span style="font-size:24px;">下面是不压位的正常版:</span>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int maxn=1010;
struct big{
	int s[maxn],len;
	big(){
		memset(s,0,sizeof(s)); len=1;
	}
	big operator=(const char *num){
		len=strlen(num);
		for(int i=0;i<len;i++) s[i]=num[len-1-i]-'0';
		return *this;
	}
	big operator=(const int num){
		char a[maxn];
		sprintf(a,"%d",num);
		*this=a;
		return *this;
	}
	big(const char *num){
		*this=num;
	}
	big (const int num){
		*this=num;
	}
	
	big operator+(const big&a) const{
		big c;
		c.len=max(len,a.len)+1;
		for(int i=0,k=0;i<c.len;i++){
			c.s[i]=k*10+s[i]+a.s[i];
			k=c.s[i]/10;
			c.s[i]%=10;
		}
		while(c.len>1 && c.s[c.len-1]==0) c.len--;
		return c;
	}
	big operator+=(const big&a){
		*this=*this+a;
		return *this;
	}
	
	big operator*(const big&a) const{
		big c;
		c.len=len+a.len;
		for(int i=0;i<len;i++)
			for(int j=0;j<a.len;j++){
				c.s[i+j]+=s[i]*a.s[j];
				c.s[i+j+1]+=c.s[i+j]/10;
				c.s[i+j]%=10;
			}
		while(c.len>1 && c.s[c.len-1]==0) c.len--;
		return c;
	}
	big operator*=(const big&a){
		*this=*this*a;
		return *this;
	}
	
	big operator/(const int a)const{
		big c;
		c.len=len;
		for(int i=len-1,k=0;i>=0;i--){
			c.s[i]=10*k+s[i];
			k=c.s[i]%a;
			c.s[i]/=a;
		}
		while(c.len>1 && c.s[c.len-1]==0) c.len--;
		return c;
	}
	big operator/=(const int a){
		*this=*this/a;
		return *this;
	}
	
	bool operator<(const big&a)const{
		if(len!=a.len) return len<a.len;
		for(int i=len-1;i>=0;i--){
			if(s[i]!=a.s[i]) return s[i]<a.s[i];
		}
	}
	bool operator>(const big&a)const{
		return a<*this;
	}
	bool operator<=(const big&a)const{
		return !(*this>a);
	}
	bool operator>=(const big&a)const{
		return !(*this<a);
	}
	bool operator==(const big&a)const{
		return !(*this<a) && !(*this>a);
	}
	bool operator!=(const big&a)const{
		return *this<a || *this>a;
	}
	
};

istream& operator>>(istream &in,big &a){
	char num[maxn];
	in>>num;
	a=num;
	return in;
}

ostream& operator<<(ostream &out,big &a){
	for(int i=a.len-1;i>=0;i--) cout<<a.s[i];
	return out;
}

int main(){
	big s(10),ss(120);
	cin>>s>>ss; 
	ss*=10;
	cout<<ss<<endl;
	return 0;
}

<span style="font-size:24px;">下面是压位(4位)的程序:</span>
<span style="font-size:24px;"></span><pre name="code" class="cpp">#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
const int maxn=1000;

struct big
{
	int len,s[maxn];
	big(){memset(s,0,sizeof(s)); len=1;}
	big operator=(const char *num)
	{
		len=strlen(num);
		memset(s,0,sizeof(s));
		for(int i=0;i<len;i++) s[(len-i-1)/4]=s[(len-i-1)/4]*10+num[i]-'0';
		len=len/4+1;
		if(len>1 && s[len-1]==0) len--;
		return *this;
	}
	big operator=(const long long num)
	{
		char a[maxn];
		sprintf(a,"%d",num);
		*this=a;
		return *this;
	}
	big (const long long num)
	{
		*this=num;
	}
	big (const char *num)
	{
		*this=num;
	}
	
	big operator+(big &a)
	{
		big c;
		c.len=max(len,a.len)+1;
		for(int i=0,x=0;i<c.len;i++)
		{
			c.s[i]=s[i]+a.s[i]+x;
			x=c.s[i]/10000;
			c.s[i]%=10000;
		}
		if(c.len>1 && c.s[c.len-1]==0) c.len--;
		return c;
	}
	big operator+=(big &a)
	{
		*this=*this+a;
		return *this;
	}
	big operator*(const big &a) const
	{
		big c;
		c.len=len+a.len;
		for(int i=0;i<len;i++)
			for(int j=0;j<a.len;j++)
			{
				c.s[i+j]+=s[i]*a.s[j];
				c.s[i+j+1]+=c.s[i+j]/10000;
				c.s[i+j]%=10000;
			}
		if(c.len>1 && c.s[c.len-1]==0) c.len--;
		return c;
	}
	
	big operator*=(const big &a)
	{
		*this=*this*a;
		return *this;
	}
	big operator/(int a)
	{
		big c;
		c.len=len;
		for(int i=len-1,k=0;i>=0;i--)
		{
			c.s[i]=10000*k+s[i];
			k=c.s[i]%a;
			c.s[i]/=a;
		}
		while(c.len>1 && c.s[c.len-1]==0) c.len--;
		return c;
	}
	big operator/=(int a)
	{
		*this=*this/a;
		return *this;
	}
	bool operator<(big a)
	{
		if(len!=a.len) return len<a.len;
		for(int i=len-1;i>=0;i--)
			if(s[i]!=a.s[i]) return s[i]<a.s[i];
		return 0;
	}
	bool operator>(big a)
	{
		return a<*this;
	}
	bool operator<=(big a)
	{
		return !(*this>a);
	}
	bool operator>=(big a)
	{
		return !(*this<a);
	}
	bool operator==(big a)
	{
		return *this<=a && *this>=a;
	}
	bool operator!=(big a)
	{
		return !(*this==a);
	}
	
};

istream &operator>>(istream &in,big &a)
{
	char num[maxn];
	in>>num;
	a=num;
	return in;
}

ostream &operator<<(ostream &out,big a)
{
	cout<<a.s[a.len-1];
	for(int i=a.len-2;i>=0;i--)
	{
		if(a.s[i]<1000) cout<<"0";
<span style="white-space:pre">		</span>if(a.s[i]<100) cout<<"0";
<span style="white-space:pre">		</span>if(a.s[i]<10) cout<<"0";
<span style="white-space:pre">		</span>cout<<a.s[i];
	}
	return out;
}

int main()
{
	int n;
	big ss,sss;
	cin>>ss>>sss;
//	cin>>n;
/*	for(int i=1;i<=n;i++)
	{
		ss=ss*i;
	}
	cout<<ss<<endl;
*/
	ss=ss*sss;
	cout<<ss<<endl;	
	return 0;
}


                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值