跳到正文

剪贴板

剪贴板 o140f7bn

原文
剪贴板信息

剪贴板信息

收录
更新

低脂高精度

加:add__ha 减:subtract__hs 乘:multiply__hm 除:divide__hd


加:

C++
string ha(string a,string b){
	string c="";
	bool carry=0;
	int res=0;
	for(int i=a.length()-1,j=b.length()-1;i>=0||j>=0;i--,j--){
		res=0;
		if(i>=0)res+=(a[i]-'0');
		if(j>=0)res+=(b[j]-'0');
		res+=int(carry),carry=res/10,c=char(res%10+'0')+c;
	}
	if(carry)c='1'+c;
	return c;
}

减:

C++
string hs(string a,string b){
	if(a==b)return "0";
	string c="";
	bool is=0,carry=0;
	if(a.length()<b.length()||(a.length()==b.length()&&a<b)){swap(a,b);is=1;}
	for(int i=a.length()-1,j=b.length()-1;i>=0||j>=0;i--,j--){
		int res=0,da=a[i]-'0'-carry,db=(j>=0)?(b[j]-'0'):0;
	    if(da>=db)res=da-db,carry=0;
	    else carry=1,res=da+10-db;
	    c=char(res+'0')+c;
	}
	for(int i=0;c[i]=='0';i++){
		if(c[i]!='0')break;
		c.erase(0,1);
	}
	if(is)c='-'+c;
	return c;
}

乘:

C++
string hm(string a,string b){
	string c="";
	int res[4001];memset(res,0,4000);
	for(int i=a.length()-1;i>=0;i--){
		for(int j=b.length()-1;j>=0;j--){
			int pro=(a[i]-'0')*(b[j]-'0'),p1=i+j+1,p2=p1-1;
			pro+=res[p1],res[p1]=pro%10,res[p2]+=pro/10;
		}
	}
	bool f=1;
	int l=a.length()+b.length();
	for(int i=0;i<l;i++)c+=char(res[i]+'0');
	while(c[0]=='0'&&c.length()>1)c.erase(0,1);
	return c;
}

除:

C++
string hd(string a,long long b){
	if(a=="0")return "0";
	if(b==1)return a;
	if(a==to_string(b))return "1";
	long long temp=0,l=a.length();
	string res="";
	for(int i=0;i<l;i++)temp=temp*10+(a[i]-'0'),res+=char(temp/b+'0'),temp%=b;
	while(res[0]=='0')res.erase(0,1);
	return res;
}

高精加减乘除:

C++