codewars / runner

Issue tracker for Code Runner
34 stars 8 forks source link

The test results are wrong for my code. #158

Closed cnyjwang closed 2 years ago

cnyjwang commented 2 years ago

This is my codes for kata (4 kyu) "Large Factorials". It worked fine when I tested them on CodeLite and (https://www.onlinegdb.com/online_c++_compiler). But it give a wrong answer when I test them on codewars online.

include

include

include

std::vector ntov (int num) { std:: vector nvec; std::string nstr = std::to_string(num);

 for (int i = nstr.size()-1; i >= 0; i--){

     nvec.push_back((int)nstr[i]-48);
 }

return nvec; }

std::vector LnMulti (std::vector nv1, std::vector nv2){

std::vector<int> prdv(nv1.size() + nv2.size());

for ( int i=0; i < (int)nv1.size(); i++) {

        for (int j=0; j < (int)nv2.size(); j++){

        prdv[i+j] += nv1[i] * nv2[j];

            }
}
//mutilplication of int vetor (no carry). The order is a reverse. 

for (int i=0; i < (int)prdv.size(); i++){
    int lf;
    int rm;

    lf = prdv[i]%10; 
    rm = prdv[i]/10; 
    prdv[i] = lf;
    prdv [i+1] += rm;  

} 

if (prdv[prdv.size()-1] == 0) {
    prdv.pop_back();

} 
  //carry mutilple digits in one element to next ones.
return prdv;

}

std::vector<int> pr={1};
std::string factorial(int factorial){

for (int i=1; i<=factorial; i++){

 pr= LnMulti(ntov(i), pr);

}

std::string prs;

for (int i=(int)(pr.size()-1); i>=0; i--){

    prs=prs+(char)(pr[i]+48);
}

return prs;

}

kazk commented 2 years ago

Please ask questions on the discussion page of the kata.