20pts,AC on #1

P1303 A*B Problem

Kinzo @ 2023-02-15 20:11:00

#include <bits/stdc++.h>
using namespace std;

int a[2010],b[2010],ans[4020];
int lena,lenb,pos=-1;
string stra,strb;

void length(int &len,string str) {
    len=str.size();
}

void change(int len,string str,int *p) {
    for(int i=1; i<=len; i++) {
        p[i]=str[len-i-1]-48;
    }
}

void init() {
    cin>>stra>>strb;
    length(lena,stra);
    length(lenb,strb);

    change(lena,stra,a);
    change(lenb,strb,b);
}

void count() {
    for(int i=1; i<=lena; i++) {
        for(int j=1; j<=lenb; j++) {
            ans[i+j-1]=a[i]*b[j];
        }
    }
}

void carry() {
    for(int i=1; i<=lena+lenb; i++) {
        ans[i+1]+=ans[i]/10;
        ans[i]%=10;
    }
}

void del_0() {
    for(int i=lena+lenb; i>=1; i--) {
        if(ans[i]>=1){
            pos=i;
            break;
        }
    }
}

void output(){
    for(int i=pos;i>=1;i--){
        cout<<ans[i];
    }
}

void judg(){
    if(pos==-1){
        cout<<0;
    }
}

void end(){
    cout<<endl; 
//  system("pause");
}

int main() {
    init();

    count();

    carry();

    del_0();

    output();

    judg();

    end();

    return 0;
}

by Adchory @ 2023-02-15 20:16:57

@hemuxuan0709 首先,你 change 转化错了,其次,你 count 函数也错了


by Kinzo @ 2023-02-15 20:35:13

哪里错了


|