求更正

P1303 A*B Problem

YearnstudioYangyi @ 2024-10-31 19:52:32

#include<bits/stdc++.h>
using namespace std;
string fanzhuan(string str){
    string temp;
    for(int t = str.size() - 1;t >= 0;t--){
        temp.push_back(str[t]);
    }
    //cout << "[Info]main.fanzhuan:" << str << "->" << temp << endl;
    return temp;
}
char i2c(int num){
    return char(num + '0');
}
char c2i(char num){
    return int(num) - '0';
}
string more0(string num,int n){
    if(num == "0" || n == 0){
        return num;
    }
    for(int t = 0;t < n;t++){
        num += '0';
    }
    return num;
}
string add(string an,string bn){
    string a = an.size() > bn.size()?fanzhuan(an):fanzhuan(bn);
    string b = an.size() <= bn.size()?fanzhuan(an):fanzhuan(bn);

    bool next_bit = false;
    string sum;
    for(int t = 0;t < a.size();t++){
        char at = a[t];
        char bt = t < b.size()?b[t]:'0';
        //cout << "[I]" << at << "+" << bt << "=";
        next_bit = bool((c2i(at) + c2i(bt) + int(next_bit)) / 10);
        //cout << "[Info]main.add:计算结果=" << c2i(at) + c2i(bt) + int(next_bit) << endl;
        char thisbit = i2c((c2i(at) + c2i(bt) + int(next_bit)) % 10);
        //cout << "[Info]main.add:求余结果=" << thisbit << endl;
        //cout << thisbit << endl;
        sum.push_back(thisbit); 
    }
    if(next_bit){
        sum.push_back('1');
    }
    //cout << "[Info]main.add: a = " << a << ",b = " << b << ",re = " << fanzhuan(sum);
    return fanzhuan(sum);
}
string nCs(int num,string b){
    //cout << "[Info]" << num << " * " << b << "=";
    string sum = "0";
    int next_bit = 0;
    /*if(num == 0 || b == "0"){
        return "0";
    }*/
    string tempb = fanzhuan(b);
    cout << "[Info]main.nCs:tempb = " << tempb << endl;
    for(int t = 0;t < b.size();t++){
        /*if(tempb[t] == '0'){
            continue;
        }*/
        int result = next_bit + num * c2i(tempb[t]);
        next_bit = result / 10;
        result %= 10;
        string tt;
        tt.push_back(i2c(result));
        sum = add(sum,more0(tt ,t));
    }
    if(next_bit != 0){
        string tt;
        tt.push_back(i2c(next_bit));
        sum = add(sum,more0(tt,sum.size()));
    }
    //cout << sum << endl;
    return sum;
}
string nCn(string a,string b){
    //bool map[10] = {false};
    //string example[10] = {"0"};
    //b = fanzhuan(b);
    string sum = "0";
    /*if(a == "0" || b == "0"){
        return "0";
    }*/
    for(int t = 0;t < a.size();t++){
        char at = a[t];
        //string temp = ;
        cout << "[Info]main.nCn:nCs=" << more0(nCs(c2i(at),b),t) << endl; 
        //temp = more0(temp,t);
        sum = add(more0(nCs(c2i(at),b),t),sum);
    }
    return sum;
}
int main(void){
    //freopen("./a.in","r",stdin);
    string a,b;
    cin >> a >> b;
    if(a.size() > b.size()){
        string temp = b;
        b = a;
        a = temp;
    }
    int zsum = 0;
    string ta = fanzhuan(a);
    string tb = fanzhuan(b);
    int t;
    for(t = 0;t < ta.size();t++){
        if(ta[t] != '0'){
            break;
        }
        zsum++;
    }
    a = "";
    for(int i = t;i <ta.size();i++){
        a.push_back(ta[i]);
    }
    for(t = 0;t < tb.size();t++){
        if(tb[t] != '0'){
            break;
        }
        zsum++;
    }
    b = "";
    for(int i = t;i <tb.size();i++){
        b.push_back(tb[i]);
    }
    a = fanzhuan(a);
    b = fanzhuan(b);
    //cout << a << endl << b << endl;
    cout << more0(nCn(a,b),zsum);
    return 0;
}

求更正,错误数据: 123 321 此时会出错


by yiyuhang123 @ 2024-11-07 11:48:43

@szy2009


#include<bits/stdc++.h>
using namespace std;
int ans[900000];

int main(){
    string s1;
    string s2;   
    cin>>s1>>s2;
    if(s1=="0"||s2=="0"){
        cout<<0;
        return 0;
    }
    int len1=s1.size();
    int len2=s2.size(); 
    int jinwei=0;
    int mod=0;
    for(int i=len2-1;i>=0;i--){
        jinwei=0;
        if(s2[i]-'0'==0){
            continue;
        }
        for(int j=len1-1;j>=0;j--){
            int mul=(s2[i]-'0')*(s1[j]-'0')+jinwei+ans[i+j+1];
            mod=mul%10;
            jinwei=mul/10; 
            ans[i+j+1]=mod;
        }
        if(jinwei!=0){
            ans[i]=jinwei;
        }
    }
    if(jinwei!=0){
        ans[0]=jinwei;
    }
    int flag=0;
    for(int i=0;i<len1+len2;i++){
        if(ans[i]!=0){
            flag=1;
        }
        if(flag){
            cout<<ans[i];
        }
    }
    if(!flag){
        cout<<0;
    }
    return 0;
}

|