题目
题目标题:
计算阶乘n!是一件可怕的事情,因为当n并不是很大时,n!将是一个很大的值。例如13! = 6227020800,已经超过了我们常用的unsigned int类型的取值范围。请设计一个程序,使其可以计算100以内的数的阶乘,结果用字符串的形式输出
详细描述:
接口说明
原型:
void CalcNN(int n, char *pOut)
输入参数:
int n 需要计算的阶乘数
输出参数:
char *pOut 结算结果,内存由调用者负责管理
返回值:
无
限制:
无
举例:
题目来源: 软件训练营
练习阶段: 中级
代码
#include <iostream>
#include <string.h>
#include "oj.h"
using namespace std;
void CalcNN(int n, char *pOut){
if(pOut == NULL || n < 0){
return;
}
int index = 0;
pOut[index++] = '1';
if(n == 1 || n == 0){
pOut[index] = '\0';
return;
}
int tmp,carry;
for(int i = 2;i <= n;++i){
carry = 0;
for(int j = 0;j < index;++j){
tmp = (pOut[j] - '0') * i + carry;
pOut[j] = tmp % 10 + '0';
carry = tmp / 10;
}
while(carry){
pOut[index++] = carry % 10 + '0';
carry /= 10;
}
}
pOut[index] = '\0';
for(int i = 0,j = index-1;i < j;++i,--j){
swap(pOut[i],pOut[j]);
}
}
<script type="text/javascript">
$(function () {
$('pre.prettyprint code').each(function () {
var lines = $(this).text().split('\n').length;
var $numbering = $('<ul/>').addClass('pre-numbering').hide();
$(this).addClass('has-numbering').parent().append($numbering);
for (i = 1; i <= lines; i++) {
$numbering.append($('<li/>').text(i));
};
$numbering.fadeIn(1700);
});
});
</script>
分享到:
相关推荐
华为机试一霸教你过华为机试演讲稿..pdf
华为OD机试(..75.rar
华为机试真题(非牛客网试练题)OD考试真题,不定期更新,文档含代码解答
华为机试一霸教你过华为机试.doc
华为机试一霸教你过华为机试。大菊厂招聘有三关,心理测试,机试,面试。
。。。
。。。
华为机试算法题总结 经验分享
华为机试oj练习题2014
华为机试一霸教你过华为机试e-18页.pdf
本人在准备2014年华为机试的时候,进行整理的,代码全部运行成功。如有错误,请大家见谅。
华为OD系列--华为OD机试
华为机试题目100题练习题
华为机试成功归来,与小伙伴们分享下经验
大师兄教你如何过华为机试
贰壹贰叁零华为OD机试.pptx 华为OD机试.pptx 华为OD机试.pptx
里面有几十道华为历届考过的机试题,可供大家研究和参考,主要是里面的思想,只要理解透了里面的思想,非常有助于机试。
华为机试.md
2014重邮华为机试(2013.9.14和2013.9.15)一共三场的题目
华为机试.py