string哪里不行?

P1303 A*B Problem

freakflowey98 @ 2020-08-18 16:48:13

啥也不说上代码

#include<iostream>
#include<string>
string num1;
string num2;
int data1[1000]={0}; 
int data2[1000]={0};
int ans[2000]={0}; 
using namespace std;
int main() {
    cin>>num1;
    cin>>num2;
    int len1=num1.length();
    int len2=num2.length();
    for (int i=0;i<len1;i++) data1[i]=num1[len1-1-i]-'0';
    for (int i=0;i<len2;i++) data2[i]=num2[len2-1-i]-'0';
    int carry=0;
    for (int i=0;i<len1;i++) {
        carry=0;
        for (int j=0;j<len2;j++) {
            // 重点: i + j
            ans[i+j]+=data1[i]*data2[j]+carry;
            carry=ans[i+j]/10;
            ans[i+j]%=10;
        }
        // 每一遍循环之后的进位
        ans[i+len2]+=carry;
    }
    int index =len1+len2;
    // 去掉前面多余的0
    while (index>0&&ans[index]==0) index--;
    for (int i=index;i>=0;i--)
        cout<<ans[i];
    return 0;
}

我自己的编译器和洛谷的都说不能用string???


by kradcigam @ 2020-08-18 16:56:04

@freakflowey98 应该这样写


by JRzyh @ 2020-08-18 16:56:04

https://www.luogu.com.cn/discuss/show/241461

https://www.luogu.com.cn/discuss/show/244341


by 清雅流水 @ 2020-08-18 16:58:53

其实也可以用std::的


by freakflowey98 @ 2020-08-19 15:23:28

草草草我忘了


上一页 |