We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
voidcoutVector(vector<int>*a){intlen=(*a).size();for(inti=0;i<len;i++){cout<<(*a)[i];}}/** * This function bring every character of * string `a` to vector `b` under int type * Input: a is a string with number characters * Input: b is a blank vector * Effect: b is changed with full of number got from * string a's number characters, but in reverse way */voidtranslateStringToVector(stringa,vector<int>*b){for(inti=a.size()-1;i>=0;i--){(*b).push_back(a[i]-48);}}vector<int>surplusVaN(vector<int>a,intn){vector<int>result{};vector<int>b{};translateStringToVector(to_string(n),&b);vector<int>*maxV{};if(a.size()>=b.size()){maxV=&a;}else{maxV=&b;}intlen=(*maxV).size();intminSize=min(a.size(),b.size());intremain=0;for(inti=0;i<len;i++){if(i>=minSize){intsum=(*maxV)[i]+0+remain;remain=sum/10;result.push_back((sum-remain*10));continue;}intsum=a[i]+b[i]+remain;remain=sum/10;result.push_back(sum-remain*10);}if(remain>0){result.push_back(remain);}returnresult;}/** * This function get * Input: `result` destination, vector `a` and * number `b` for calculation. Vector `a` is reverse * It returns * Output: `result` have new value that each * number in vector is changed */voidmultiplyVaN(vector<int>*result,vector<int>a,intb){intlen=a.size();vector<int>remain{0};for(inti=0;i<len;i++){vector<int>product=surplusVaN(remain,a[i]*b);remain.clear();if(product.size()>1){remain.insert(remain.begin(),product.begin()+1,product.end());// Bring `remain` to next index}(*result)[i]=product[0];// Get the last position number of product;}if(remain.size()==1&&remain[0]!=0){(*result).push_back(remain[0]);}if(remain.size()>1){(*result).insert((*result).end(),remain.begin(),remain.end());}}voidextraLongFactorials(intn){vector<int>result{1};for(inti=1;i<=n;i++){multiplyVaN(&result,result,i);}reverse(result.begin(),result.end());coutVector(&result);}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Extra Long Factorials
You are viewing a single comment's thread. Return to all comments →
C++ Solution: