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

Problem Statement
You are given an integer N. Print the factorial of this number.
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的更多相关文章
- 每日一九度之 题目1038:Sum of Factorials
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2109 解决:901 题目描述: John von Neumann, b. Dec. 28, 1903, d. Feb. 8, 1957, ...
- POJ 1775 (ZOJ 2358) Sum of Factorials
Description John von Neumann, b. Dec. 28, 1903, d. Feb. 8, 1957, was a Hungarian-American mathematic ...
- Codewars, Leetcode, Hackerrank. Online Judges Reviews
http://jasonjl.me/blog/2015/03/30/practical-programming-practice-services/ Codewars, Leetcode, Hacke ...
- 九度OJ 1038:Sum of Factorials(阶乘的和) (DP、递归)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1845 解决:780 题目描述: John von Neumann, b. Dec. 28, 1903, d. Feb. 8, 1957, ...
- 1130mysql explain中的type列含义和extra列的含义
很多朋友在用mysql进行调优的时候都肯定会用到explain来看select语句的执行情况,这里简单介绍结果中两个列的含义. 1 type列 官方的说法,说这列表示的是"访问类型" ...
- 日常小测:颜色 && Hackerrank Unique_colors
题目传送门:https://www.hackerrank.com/challenges/unique-colors 感谢hzq大神找来的这道题. 考虑点分治(毕竟是路经统计),对于每一个颜色,它的贡献 ...
- 如何在Mac系统里面更新 Ansible 的 Extra Modules
最近遇到一个问题 seport is not a legal parameter in an Ansible task or handler 原因是我本地 Ansible 的 Extra Module ...
- 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' ...
- Hackerrank Going to the Office
传送门 Problem Statement Ms.Kox enjoys her job, but she does not like to waste extra time traveling to ...
随机推荐
- PHP版本VC6和VC9、Non Thread Safe和Thread Safe的区别
链接:http://www.cnblogs.com/neve/articles/1863853.html 想更新个PHP的版本,PHP的windows版本已经分离出来了,见http://windows ...
- U3D assetbundle加载
using UnityEngine; using System.Collections; public class testLoadFromAB : MonoBehaviour { IEnumerat ...
- MVC4验证用户登录特性实现方法
在开发过程中,需要用户登陆才能访问指定的页面这种功能,微软已经提供了这个特性. // 摘要: // 表示一个特性,该特性用于限制调用方对操作方法的访问. [AttributeUsage(Attribu ...
- scrapy 代理
说明: 本文参照了官网文档,以及stackoverflow的几个问题 概要: 在scrapy中使用代理,有两种使用方式 使用中间件 直接设置Request类的meta参数 方式一:使用中间件 要进行下 ...
- pandas 时间序列resample
resample与groupby的区别:resample:在给定的时间单位内重取样groupby:对给定的数据条目进行统计 函数原型:DataFrame.resample(rule, how=None ...
- Android -- Properties使用
import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.Properties; public ...
- iOS:界面适配(三)--iPhone不同机型或设备不同尺寸适配(屏幕适配)和系统适配
对于不同苹果设备,各个参数查看<iOS:机型参数.sdk.xcode各版本>. 机型变化 坐标:表示屏幕物理尺寸大小,坐标变大了,表示机器屏幕尺寸变大了: 像素:表示屏幕图片 ...
- 利用JS跨域做一个简单的页面访问统计系统
其实在大部分互联网web产品中,我们通常会用百度统计或者谷歌统计分析系统,通过在程序中引入特定的JS脚本,然后便可以在这些统计系统中看到自己网站页面具体的访问情况.但是有些时候,由于一些特殊情况,我们 ...
- 九幽史程博:助力国内开发者借Win10东风出海
微软Biuld2016大会刚刚结束,会议上微软CEO纳德拉Show出的一大波黑科技,又一次让软粉们心情为之振奋,信仰充值爆棚! 尽管过去一年微软的Win10 Mobile表现不尽如人意,可是凭借PC端 ...
- 新年奉献MVC+EF(CodeFirst)+Easyui医药MIS系统
本人闲来无事就把以前用Asp.net做过的一个医药管理信息系统用mvc,ef ,easyui重新做了一下,业务逻辑简化了许多,旨在加深对mvc,ef(codefirst),easyui,AutoMap ...