Use an array to calculate the factorial of n (n is relatively large) and the number of 0 at the end

Source: Internet
Author: User
// Use an array to calculate the factorial of N (where N is relatively large) and the number of 0 at the end // convert from: struct <iostream> # include <vector> # include <algorithm> using namespace STD; string add (string num1, string num2) {// large addition string result = ""; int len1 = num1.length (); int len2 = num2.length (); int naddon = 0; // carry int I, j, N1, N2, sum; vector <char> tmpsum; for (I = len1-1, j = len2-1; I> = 0 & J> = 0; -- I, -- J) {n1 = num1 [I]-'0'; N2 = num2 [J]-'0'; sum = N1 + N2 + naddon; If (sum> = 10) naddon = 1; elsenaddon = 0; tmpsum. push_back (sum % 10 + '0');} If (len1> len2) {// The first one has the remaining for (; I> = 0; -- I) {n1 = num1 [I]-'0'; sum = N1 + naddon; If (sum> = 10) naddon = 1; elsenaddon = 0; tmpsum. push_back (sum % 10 + '0') ;}} else if (len2> len1) {// The second for (; j> = 0; -- J) {n2 = num2 [J]-'0'; sum = n2 + naddon; If (sum> = 10) naddon = 1; elsenaddon = 0; tmpsum. push_back (sum % 10 + '0');} If (naddon> 0) {tmpsum. push_back (naddon + '0');} // because the result is inverted, reverse (tmpsum. begin (), tmpsum. end (); copy (tmpsum. begin (), tmpsum. end (), back_inserter (result); return result;} string multipy (string num1, string num2) {// multiplication of large numbers, multiplication is essentially the addition of the accumulated string result = "0"; int I, j, N1, N2; int len1 = num1.length (); int len2 = num2.length (); if (len1 <len2) {for (I = len1-1; I> = 0; -- I) {n1 = num1 [I]-'0 '; string sum = "0"; for (j = 0; j <N1; ++ J) sum = add (sum, num2); string tmpsb (SUM ); for (j = I; j <len1-1; ++ J) tmpsb. append ("0"); Result = add (result, tmpsb) ;}} else {for (I = len2-1; I> = 0; -- I) {n2 = num2 [I]-'0'; string sum = "0"; for (j = 0; j <N2; ++ J) sum = add (sum, num1); string tmpsb (SUM); For (j = I; j <len2-1; ++ J) tmpsb. append ("0"); Result = add (result, tmpsb) ;}} return result;} string factorial (int n) {string result = "1 "; char buff [100]; for (INT I = N; I> = 2; -- I) {result = multipy (result, ITOA (I, buff, 10 ));} return result;} int countzero (string res) {char ch [100]; int COUNT = 0; int Len = res. length (); char * P = CH + len-1; strcpy (CH, Res. c_str (); While (* P = '0') {count ++; p --;} return count;} int func (int n) {If (n> 0 & n <5) return 0; else {int K = N/5; return K + func (k) ;}} int main () {int N; CIN> N; int count; string STR = factorial (n); cout <Str. c_str () <Endl; Count = countzero (STR); cout <count <Endl; cout <func (n) <Endl; return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.