60分求助,最后两个点wa了 (高精度乘法)
  • 板块P1303 A*B Problem
  • 楼主QODGOD
  • 当前回复0
  • 已保存回复0
  • 发布时间2021/3/30 14:12
  • 上次更新2023/11/5 01:22:40
查看原帖
60分求助,最后两个点wa了 (高精度乘法)
272309
QODGOD楼主2021/3/30 14:12
#include<bits/stdc++.h>
using namespace std;

string  add(string str1,string str2)
{
	string str;
	//补齐 
	int len1=str1.length();
	int len2=str2.length();
	if(len1>len2){
		for(int i=1;i<=(len1-len2);i++){
			str2="0"+str2;
		}
	}
	else{
		for(int i=1;i<=(len2-len1);i++){
			str1="0"+str1;
		}
	len1=str1.length();
	
	//temp
	int temp;
	int cf=0;
	for(int i=len1-1;i>=0;i--){
		temp=(str1[i]-'0')+(str2[i]-'0')+cf;
		cf=temp/10;
		temp%=10;
		str=char(temp+'0')+str;
	} 
	if(cf!=0) str=char(cf+'0')+str;
	}
	
	return str;
} 

string  mul (string str1,string str2){
	if(str1=="0"||str2=="0") return "0";
	
	string str;
	int len1=str1.length();
	int len2=str2.length();
	string tempstr;
	int temp,cf;	
	for(int i=len2-1;i>=0;i--){	
		cf=0;
		tempstr="";
		temp=str2[i]-'0';
		int t=0;
		  if(temp!=0)
        {
            for(int j=1;j<=len2-1-i;j++)
              tempstr+="0";
            for(int j=len1-1;j>=0;j--)
            {
                t=(temp*(str1[j]-'0')+cf)%10;
                cf=(temp*(str1[j]-'0')+cf)/10;
                tempstr=char(t+'0')+tempstr;
            }
            if(cf!=0) tempstr=char(cf+'0')+tempstr;
        }
		str=add(str,tempstr);
	
	}

	return str;
}

int main(){
	string str1,str2,str;
	cin>>str1>>str2;
	str=mul(str1,str2);
	
2021/3/30 14:12
加载中...