Topic
题目标题:计算阶乘n!是一件可怕的事情,因为当n并不是很大时,n!将是一个很大的值。例如13! = 6227020800,已经超过了我们常用的unsigned int类型的取值范围。请设计一个程序,使其可以计算100以内的数的阶乘,结果用字符串的形式输出详细描述:接口说明原型:void CalcNN(int n, char *pOut)输入参数:int n 需要计算的阶乘数输出参数:char *pOut 结算结果,内存由调用者负责管理返回值:无限制:无举例:题目来源: 软件训练营 练习阶段:
Code
/* ---------------------------------------* Date: 2015-06-30* sjf0115* title: Horrible factorial * Source: Huawei on-Machine-------------------------- ---------------*/#include <iostream>#include <string.h>#include "oj.h"using namespace Std;voidCALCNN (intNChar*pout) {if(POut = = NULL | | n <0){return; }//if int Index=0; pout[Index++] =' 1 ';if(n = =1|| n = =0) {pout[Index] =' + ';return; }//if //calculate factorial intTmp,carry; for(inti =2; I <= n;++i) {//Carry clear 0carry =0;//I multiplies each bit of the current calculated factorial result for(intj =0; J <Index; ++j) {tmp = (Pout[j]-' 0 ') * i + carry; POUT[J] = tmp%Ten+' 0 '; carry = tmp/Ten; }//for //And rounding while(Carry) {pout[Index+ +] = carry%Ten+' 0 '; Carry/=Ten; }//while}//ifpout[Index] =' + ';//Flip for(inti =0, j =Index-1; I < j;++i,--j) {Swap (pout[i],pout[j]); }//for}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
[Huawei Machine Test exercises]17. Horrible factorial