传送门

今天在HackerRank上翻到一道高精度题,于是乎就写了个高精度的模板,说是模板其实就只有乘法而已。

Extra long factorials

Authored by vatsalchanana on Jun 16 2015

Problem Statement

You are given an integer N. Print the factorial of this number.

N!=N×(N−1)×(N−2)×⋯×3×2×1

Note: Factorials of N>20 can't be stored even in a 64−bit long long variable. Big integers must be used for such calculations. Languages like Java, Python, Ruby etc. can handle big integers but we need to write additional code in C/C++ to handle such large values.

We recommend solving this challenge using BigIntegers.

Input Format 
Input consists of a single integer N.

Constraints 
1≤N≤100

Output Format 
Output the factorial of N.

Sample Input

25

Sample Output

15511210043330985984000000
-------------------------------------------------------------------------------------
从前看C++ Primer时,看到Constructor处发现有很多方便的feature,但一般也不写Class,大多忘却了。这回用了一下,还不错。
const int N();
int tmp[N];
struct BigInt{
int d[N], len;
BigInt(int n){
len=!n;
for(int tmp=n; tmp; len++, tmp/=);
for(int i=len-; i>=; i--)
d[i]=n%, n/=;
}
BigInt(){}
void multi(const BigInt &n){
memset(tmp, , sizeof(tmp));
for(int i=; i<len; i++)
for(int j=; j<n.len; j++)
tmp[i+j]+=d[i]*n.d[j];
for(int i=len+n.len-; i; i--)
tmp[i-]+=tmp[i]/, tmp[i]%=;
int _len=len+n.len-;
*this=BigInt(tmp[]);
for(int i=; i<=_len; i++)
d[len++]=tmp[i];
}
void print(){
for(int i=; i<len; i++)
printf("%d", d[i]);
puts("");
}
};

注意到这里写了两个Constructor,一个带参数的BigInt(int n)以及一个不带参数的BigInt()。如果一个class里没写constructor的话其实还是带有一个default constructor的,这里就是BigInt::BigInt(),但如果定义了Constructor的话default constructor就会被覆盖,所以这里还要将BigInt()显式(explicitly)定义一下,否则 BigInt a;这种声明变量的方式就会报错。

和constructor有关的概念还有type conversion。我们定义了BigInt::BigInt(int n)后求阶乘就可很简洁地写成

    BigInt res();
for(int i=; i<=n; i++){
res.multi(i);
}

我们将int型的变量i传递给函数BigInt::void multi(const BigInt &n)时,int将会自动转换成BigInt,之所以可以这样正是由于我们定义了constructor BigInt(int n) 

HackerRank Extra long factorials的更多相关文章

  1. 每日一九度之 题目1038:Sum of Factorials

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2109 解决:901 题目描述: John von Neumann, b. Dec. 28, 1903, d. Feb. 8, 1957, ...

  2. POJ 1775 (ZOJ 2358) Sum of Factorials

    Description John von Neumann, b. Dec. 28, 1903, d. Feb. 8, 1957, was a Hungarian-American mathematic ...

  3. Codewars, Leetcode, Hackerrank. Online Judges Reviews

    http://jasonjl.me/blog/2015/03/30/practical-programming-practice-services/ Codewars, Leetcode, Hacke ...

  4. 九度OJ 1038:Sum of Factorials(阶乘的和) (DP、递归)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1845 解决:780 题目描述: John von Neumann, b. Dec. 28, 1903, d. Feb. 8, 1957, ...

  5. 1130mysql explain中的type列含义和extra列的含义

    很多朋友在用mysql进行调优的时候都肯定会用到explain来看select语句的执行情况,这里简单介绍结果中两个列的含义. 1 type列 官方的说法,说这列表示的是"访问类型" ...

  6. 日常小测:颜色 && Hackerrank Unique_colors

    题目传送门:https://www.hackerrank.com/challenges/unique-colors 感谢hzq大神找来的这道题. 考虑点分治(毕竟是路经统计),对于每一个颜色,它的贡献 ...

  7. 如何在Mac系统里面更新 Ansible 的 Extra Modules

    最近遇到一个问题 seport is not a legal parameter in an Ansible task or handler 原因是我本地 Ansible 的 Extra Module ...

  8. mac上执行sed的编辑 -i命令报错sed: 1: "test.txt": undefined label ‘est.txt’或sed: 1: "2a\test\": extra characters after \ at the end of a command

    问题一 sed编辑命令:[sed -i 's/a/b/g' test.txt]   报错:sed: 1: "test.txt": undefined label 'est.txt' ...

  9. Hackerrank Going to the Office

    传送门 Problem Statement Ms.Kox enjoys her job, but she does not like to waste extra time traveling to ...

随机推荐

  1. finder怎么才能找到library

    右键Finder——前往目录 输入~/Library

  2. 25个实用的jquery技巧

    1. 去除页面的右键菜单   $(document).ready(function(){ $(document).bind(“contextmenu”,function(e){returnfalse; ...

  3. 将TP引擎改为smarty引擎

    在common/config文件里设置'TMPL_ENGINE_TYPE'=>'Smarty'即可,但要注意,在模板文件里的css样式{}要用一对{literal}{/literal}标签包裹, ...

  4. iOS原生地图开发进阶——使用导航和附近兴趣点检索

    iOS原生地图开发进阶——使用导航和附近兴趣点检索 iOS中的mapKit框架对国际化的支持非常出色.在前些篇博客中,对这个地图框架的基础用法和标注与覆盖物的添加进行了详细的介绍,这篇博客将介绍两个更 ...

  5. window.lacation.replace

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  6. DevExpress主从表 按组分页一组不足一页为一页--以此记录

    本文的主要是说明Dev的报表的主从表,主从表的每一组显示在一页,当一组超出一页,第二页只显示第一组的. 一.每上报表设置图 简单设计图如上 二.后台代码 报表页代码 public partial cl ...

  7. VC/Wince 实现仿Win8 Metro风格界面3——按钮移动交换、删除、添加快捷方式(附效果图)

    上一篇文章写了如何进行页面滑动切换,今天我讲一下如何实现两个按钮拖动交换位置,包括同一个页面按钮交换或者两个页面之间的按钮交换.另外就是如何拖动删除界面上的快捷方式.按钮交换和拖动删除,这两个功能基本 ...

  8. discuz编码转换UTF8与GBK互转完美适合Discuz3.x系列

    由于一些网站通信编码的问题不得不把一直使用的网站编码由UTF8转为GBK,在转换过程中在官方看了很多方法,自己也都尝试了一些最后都没有能够成功,数据库的转换一直都是没有大问题,不存在丢失什么的,能看到 ...

  9. 微信第一个“小程序”亮相:不是APP胜似APP!

    前天晚上,微信终于推出了“小程序”功能.看过效果演示之后,网友表示,好多App可以卸载了! 据了解,微信“小程序”已首批开放给200名拥有微信服务号的开发者进行内测,而且目前开发者发布的小程序无法在用 ...

  10. 最新app store 应用提交经验分享

    由于之前提交实在3月份的时候,后来长时间没有提交了,最近又需要提交,发现苹果已经发生翻天覆地的变化了,真是跟不上时代了啊.... 之前提交的基本也是从网上看的,前面的证书安装部分其实基本是一样的没什么 ...