洛谷-高精题A*B

    科技2022-07-11  92

    codeblocks能通过来着。。。 先记下来,明天找人问问。

    #include<bits/stdc++.h> using namespace std; string add(string a,string b){ string c=""; int len1=a.length(); int len2=b.length(); //前面补0,弄成长度相等 if(len1<len2){ for(int i=1;i<=len2-len1;i++) a="0"+a; } else{ for(int i=1;i<=len1-len2;i++) b="0"+b; } len1=a.length(); int cf=0; int temp; for(int i=len1-1;i>=0;i--){ temp=a[i]-'0'+b[i]-'0'+cf; //cf=temp/10; //temp=temp; cf=0; if(temp>=10){ cf=1; temp=temp-10; } c=char(temp+'0')+c;//不太懂 } if(cf!=0) c=char(cf+'0')+c; return c; } string mutli(string a,string b){ string c=""; string ctemp; int a1=a.length(); int b1=b.length(); int k=0;//每循环一次乘10; int temp; int jw; for(int i=b1-1;i>=0;i--){ temp=0; jw=0; ctemp=""; for(int j=a1-1;j>=0;j--){ temp=(b[i]-'0')*(a[j]-'0')+jw; temp=temp%10; jw=temp/10; ctemp=char(temp+'0')+ctemp; } for(int i=0;i<k;i++){ ctemp=ctemp+'0'; } c=add(c,ctemp); k++; } return c; } int main(){ string a,b;// cin>>a>>b; string c=mutli(a,b); for(int i=0;i<c.length();i++){ int j=c[i]-'0'; cout<<j; } //cout<<mutli(a,b)<<endl; return 0; }
    Processed: 0.014, SQL: 8