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:
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)