[Huawei Machine Test exercises]17. Horrible factorial

Source: Internet
Author: User

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

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.