why?????????

P1303 A*B Problem

OIer_hzn @ 2022-12-30 12:27:40

//
//  main.cpp
//  c++
//
//  Created by aa on 2022/12/30.
//
#include <iostream>
#include <cstdio>
using namespace std;
const int maxn=1e5;
bool cmp(string a,string b){
    if(a.size()!=b.size()) return a.size()<b.size();
    return a<b;
}
string add(string num1, string num2) {
    int a1[maxn] = {0};
    int a2[maxn] = {0};
    int len1 = num1.size();
    int len2 = num2.size();
    for(int i=0;i<len1;i++) a1[i]=num1[len1-1-i]-'0';
    for(int i=0;i<len2;i++) a2[i]=num2[len2-1-i]-'0';
    len1 = max(len1,len2);
    for (int i=0;i<len1;i++) a1[i] += a2[i];
    for (int i=0;i<len1;i++) {
        a1[i+1]+=a1[i]/10;
        a1[i]%=10;
    }while(a1[len1]) {
        a1[len1 + 1] += a1[len1] / 10;
        a1[len1] %= 10;
        len1++;
    }
    string ans = "";
    for (int i = len1-1; i >=0; i--) ans += a1[i] + '0';
    return ans;
}
string minu(string num1,string num2){
    int a1[10000]={0};
    int a2[10000]={0};
    bool sgn;
    string ans="";
    if(cmp(num1,num2)){
        sgn=true;
        swap(num1,num2);
    }// 判断谁大
    int len1 = num1.size();
    int len2 = num2.size();
    for(int i=0;i<len1;i++) a1[i]=num1[len1-1-i]-'0';
    for(int i=0;i<len2;i++) a2[i]=num2[len2-1-i]-'0';//转换成数字
    for(int i=0;i<len1;i++) a1[i] -= a2[i];
    for(int i=0;i<len1;i++){
        while(a1[i]<0){
            a1[i+1]--;
            a1[i]+=10;
        }
    }// 处理退位
    while (len1 > 1 && a1[len1 - 1] == 0) len1--;// 处理前导零。
    if (sgn) ans+='-';
    for (int i=len1-1;i>=0;i--) ans+=(a1[i]+'0');
    return ans;
}
string cheng1(string num,int x){
    string ans="";
    int len=num.size();
    int a[maxn]={0};
    for (int i=0;i<len;i++) a[i]=num[len-1-i]-'0';
    for (int i=0;i< len;i++)  a[i] *= x;
    for (int i=0;i< len;i++){
        a[i + 1] += a[i] / 10;
        a[i] %= 10;
    }while(a[len]) {
        a[len+1]+=a[len] / 10;
        a[len]%=10;
        len++;
    }
    for(int i=len-1;i>=0;i--) ans+=a[i]+'0';
    return ans;
}
string cheng2(string a,string b){
    string sum="";
    int a1[maxn]={0};
    int a2[maxn]={0};
    int ans[maxn]={0};
    int len1=a.size();
    int len2=b.size();
    int len=len1+len2-1;
    for(int i=0;i<len1;i++) a1[i]=a[len1-i-1]-'0';
    for(int i=0;i<len2;i++) a2[i]=b[len2-i-1]-'0';
    for(int i=0;i<len1;i++){
        for(int j=0;j<len2;j++){
            ans[i+j]+=a1[i]*a2[j];
        }
    }for(int i=0;i<len;i++){
        ans[i+1]+=ans[i]/10;
        ans[i]%=10;
    }while(ans[len]){
        ans[len+1]+=ans[len]/10;
        ans[len]%=10;
        len++;
    }
    for(int i=len-1;i>=0;i--) sum+=ans[i]+'0';
    return sum;
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    string a,b;
    cin>>a>>b;
    cout<<cheng2(a,b)<<endl;
    return 0;
}

|