关于代码

P1303 A*B Problem

xiao__ @ 2023-08-15 21:58:54

#include<bits/stdc++.h>
using namespace std;
int numa[500005],numb[500005],ans[1000005];string a,b;
signed main(){

    cin>>a>>b;
    for(int i=0;i<a.size();i++){
        numa[a.size()-i]=a[i]-'0';
    }
    for(int i=0;i<b.size();i++){
        numb[b.size()-i]=b[i]-'0';
    }
    int len=a.size()+b.size();
    for(int i=1;i<=a.size();i++){
        for(int j=1;j<=b.size();j++){
            ans[i+j+1]+=numa[i]*numb[j];
        }
    }
    for(int i=1;i<=len;i++){
        ans[i+1]+=ans[i]/10;
        ans[i]%=10;
    }
    while(ans[len]==0&&len>1){
        len--;
    }
    for(int i=len;i>=1;i--){
        cout<<ans[i];
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
string a, b;
int na[2005], nb[2005], ans[4100], len;
int main(){
    cin >> a >> b;
    for(int i = 0; i < a.size(); i++){
        na[a.size() - i] = a[i] - '0';
    }
    for(int i = 0; i < b.size(); i++){
        nb[b.size() - i] = b[i] - '0';
    }
    len = a.size() + b.size();
    for(int i = 1; i <= a.size(); i++){
        for(int j = 1; j <= b.size(); j++){
            ans[i + j - 1] += na[i] * nb[j];
        }
    }
    for(int i = 1; i <= len; i++){
        ans[i + 1] += ans[i] / 10;
        ans[i] %= 10;
    }
    while(ans[len] == 0 && len > 1){
        len--;
    }
    for(int i = len; i >= 1; i--){
        cout << ans[i];
    }
    return 0;
}

思路感觉一样,为什么第一个20第二个100


by what_can_I_do @ 2023-08-15 22:06:56

@chatGPT_ 上面的第16行是ans[i+j-1]下面的是ans[i+j+1]


by what_can_I_do @ 2023-08-15 22:07:33

@what_can_I_do 打反了


by xiao__ @ 2023-08-15 22:09:51

@what_can_I_do 谢谢谢谢谢谢眼睛花了


|