传送门

今天在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. 08Mybatis_入门程序——增加用户的操作以及返回自增主键的功能以及返回非自增主键的功能

    本文要实现的功能是:给user表增加一个用户. 建表如下:

  2. c# 调用打印机

    1.本地打印机 //添加引用并using System.Management; public static void AvailablePrinters() { ManagementScope ms ...

  3. 【MFC】无边框窗体 WS_THICKFRAME

    1.没有WS_THICKFRAME,有WS_SYSMENU,就没办法通过相应WM_NCHITTEST,去处理窗口的缩放.2.没有WS_THICKFRAME,也没有WS_SYSMEN,在xp系统下任务栏 ...

  4. android Camera 如何判断当前使用的摄像头是前置还是后置

    现在 android 平台的智能手机一般都标配有两颗摄像头.在 Camera 中都存在摄像头切换的功能. 并且有一些功能前后置摄像头上会有所不同.譬如人脸检测,人脸识别,自动对焦,闪光灯等功能, 如果 ...

  5. android Camera 中添加一种场景模式

    转自:http://blog.csdn.net/fulinwsuafcie/article/details/8833652 首先,来了解一下什么是场景模式. 最简单的方法当然是google了,这里有一 ...

  6. C语言 结构体中属性的偏移量计算

    //计算结构体偏移量 #include<stdio.h> #include<stdlib.h> #include<string.h> //详解:对于offscfof ...

  7. C语言 数组类型与数组指针类型

    //数组类型与数组指针类型 #include<stdio.h> #include<stdlib.h> #include<string.h> void main(){ ...

  8. 挖Linux中的古老缩略语

    [2005-06-22 15:23][Nigel McFarlane][TechTarget] <<阅读原文>> Unix已经有35年历史了.许多人认为它开始于中世纪,这个中世 ...

  9. windows7 64位安装mysql 5.7.11 zip压缩版

    现在,MySQL官网只提供zip的包了, 第一点:解压到自己的任意文件夹 注意:虽然我没有试,但尽量路径中不要有中文吧 第二点:添加环境变量 D:\web\mysql-5.7.11-winx64\bi ...

  10. 信息安全系统设计基础_exp3

    北京电子科技学院(BESTI) 实     验    报     告 课程:信息安全系统设计基础 班级:1353 姓名:吴子怡.郑伟 学号:20135313.20135322 指导教师: 娄嘉鹏 实验 ...