超时代码求改QWQ(新手已死亡)为啥string会超时啊?

P1303 A*B Problem

GrayphenDyxen @ 2024-12-06 18:29:22

用的string,改过数组大小(都改成了20),但是连1*2都算不出来

#include <bits/stdc++.h>
using namespace std;
string s1="";
string s2="";
int a[100010]={0};
int b[100010]={0};
int c[10001000]={0};
int main(){
    cin>>s1>>s2;
    for (int i=0;i<s1.size();i++){
        a[i]=s1[s1.size()-1-i]-48;
    }
    for (int i=0;i<s2.size();i++){
        b[i]=s2[s2.size()-1-i]-48;
    }   
    for (int i=0;i<s2.size();i++){
        for (int j=0;j<s1.size();i++){
            c[j+i]=c[j+i]+a[j]*b[i];
            if (c[j+i]>=10){
                c[j+i+1]+=c[j+i]/10;
                c[j+i]=c[j+i]%10;
            }
        }
    }
    int index=0;
    int len=s1.size()+s2.size();
    for (int i=len-1;i>0;i--){
        if (c[i]!=0){
            index=i;
            break;
        }
    }
    for (int i=index;i>=0;i--){
        cout<<c[i];
    }   
}

by ljcnoi @ 2024-12-06 18:40:26

把string换成char存试一试 @GrayphenDyxen


by PeppaYang @ 2024-12-06 18:44:30

vector


by GrayphenDyxen @ 2024-12-06 18:45:36

@ljcnoi char的长度怎么表示呀?


by PeppaYang @ 2024-12-06 18:46:12

用vector不用再“变”成数字了


by PeppaYang @ 2024-12-06 18:46:55

还不需要算大小


by ljcnoi @ 2024-12-06 18:50:04

@PeppaYang 不是哥们,你vector怎么方便输入呢?


by GrayphenDyxen @ 2024-12-06 18:52:07

@ljcnoi vector函数压根没学(qaq)我们还没学到……


by PeppaYang @ 2024-12-06 18:52:31

cin>>x;
v.push_back(x);

就完啦


by DDD_et @ 2024-12-06 18:53:57

@ljcnoi

我们学的就是vector的,用string输入再倒序存入啊


by PeppaYang @ 2024-12-06 18:54:10

更准确的说是

for(int i=0;i<st.size();i++){
    x.push_back(st[i]-'0');
}

| 下一页