Huawei OJ Software Training Problem (intermediate)--scary factorial (large number processing)

Source: Internet
Author: User

Topic title:

    • Calculating factorial n! is a terrible thing, because when n is not very large, n! will be a very large value. For example 13! = 6227020800, has exceeded the range of values that we commonly use for the unsigned int type. Please design a program so that it can calculate the factorial of a number within 100, and the result is output in the form of a string

Detailed Description:

    • Interface description

Prototype:

void calcnn (int n, char *pOut)

Input parameters:

int N the order multiplier that needs to be calculated

Output parameters:

Char *pOut settlement results, memory is managed by the caller

return value:

No

Limit:

No

Example:


The basic study of this type of topic is the large integer calculation, and the conversion between integers and characters, careful good, no complicated algorithm thought.

#include <iostream>//#include <string>//#include <algorithm>//#include <cmath>//#include <vector>//#include <stack>//#include <iomanip>using namespace std;        void calcnn (int n, char *pout) {int i=0,j;int C,tmp=n,slen;char ct;if (n==0 | | n==1) {pout[0]= ' 1 ';p out[1]=0;return;} while (TMP) {pout[i++]= (tmp%10) + ' 0 '; tmp=tmp/10;} Pout[i]=0;slen=strlen (POut);//cout<<slen<<endl;for (i=n-1;i>1;i--) {c=0;for (j=0;j<slen;j++) { tmp=i* (pout[j]-' 0 ') +c;if (tmp>9) {pout[j]= (tmp%10) + ' 0 '; c=tmp/10;} else {pout[j]=tmp+ ' 0 '; c=0;}} if (c!=0) {while (c) {pout[slen++]= (c%10) + ' 0 '; c/=10;} pout[slen]=0;} cout<<i<< "" <<pout<<endl;} for (i=0,j=slen-1;i<j;i++,j--) {ct=pout[j];p out[j]=pout[i];p out[i]=ct;} return;} int main () {char *ss= (char *) malloc (1000*sizeof (char));    CALCNN (the SS); Cout<<ss<<endl;return 0;}




Huawei OJ Software Training Problem (intermediate)--scary factorial (large number processing)

Related Article

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.