hdu 1041 (OO approach, private constructor to prevent instantiation, sprintf) 分类: hdoj 2015-06-17 15:57 25人阅读 评论(0) 收藏
a problem where OO seems more natural to me, implementing a utility class not instantiable.
how to prevent instantiation, thanks to http://stackoverflow.com/questions/10558393/prevent-instantiation-of-an-object-outside-its-factory-method/10558404#10558404
#include <cstdio>
#include <algorithm>
class CompTrans {
CompTrans() { }
static const unsigned NUMS_LEN=240000, MAX_NUM=1000, BASE=1000000000;
static bool isinitialized;
static char nums[NUMS_LEN];
static char* pos[MAX_NUM+2];
public:
static bool isinited() { return isinitialized; }
static void init();
static void printnth(unsigned n) { puts(pos[n]); }
static unsigned getMAX_NUM() { return MAX_NUM; }
};
bool CompTrans::isinitialized=false;
char CompTrans::nums[];
char* CompTrans::pos[];
void CompTrans::init() {
if(isinited()) return;
int i,j,k;
char *p=nums;
const int NSIZE=40;
unsigned n1[NSIZE]={0}, n2[NSIZE]={1}, n3[NSIZE]={0},tmp, *p1,*p2,*p3,*ptmp, carry;
p1=n1,p2=n2,p3=n3;
for(k=0, i=1;i<=MAX_NUM;++i) {
pos[i]=p;
p+=sprintf(p,"%u",p1[k]);
for(j=k-1;j>=0;--j) p+=sprintf(p,"%09u",p1[j]);
*p++=0;
if(p2[k+1]!=0) ++k;
for(carry=0, j=0;j<=k;++j) {
tmp=carry+p2[j]+(p1[j]<<1);
carry=tmp/BASE;
p3[j]=tmp%BASE;
}
p3[j]=carry;
ptmp=p1; p1=p2; p2=p3; p3=ptmp;
}
isinitialized=true;
}
int main() {
//freopen("input.txt","r",stdin);
int n;
if(!CompTrans::isinited()) CompTrans::init();
while(scanf("%d",&n)!=EOF && n<=CompTrans::getMAX_NUM()) {
CompTrans::printnth(n);
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。// p.s. If in any way improment can be achieved, better performance or whatever, it will be well-appreciated to let me know, thanks in advance.
hdu 1041 (OO approach, private constructor to prevent instantiation, sprintf) 分类: hdoj 2015-06-17 15:57 25人阅读 评论(0) 收藏的更多相关文章
- HDU 2101 A + B Problem Too 分类: ACM 2015-06-16 23:57 18人阅读 评论(0) 收藏
A + B Problem Too Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- hdu 1057 (simulation, use sentinel to avoid boudary testing, use swap trick to avoid extra copy.) 分类: hdoj 2015-06-19 11:58 25人阅读 评论(0) 收藏
use sentinel to avoid boudary testing, use swap trick to avoid extra copy. original version #include ...
- Hdu 1506 Largest Rectangle in a Histogram 分类: Brush Mode 2014-10-28 19:16 93人阅读 评论(0) 收藏
Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- Hdu 1010 Tempter of the Bone 分类: Translation Mode 2014-08-04 16:11 82人阅读 评论(0) 收藏
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- Hdu 1009 FatMouse' Trade 分类: Translation Mode 2014-08-04 14:07 74人阅读 评论(0) 收藏
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1532 Drainage Ditches 分类: Brush Mode 2014-07-31 10:38 82人阅读 评论(0) 收藏
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- Hdu 1507 Uncle Tom's Inherited Land* 分类: Brush Mode 2014-07-30 09:28 112人阅读 评论(0) 收藏
Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- one recursive approach for 3, hdu 1016 (with an improved version) , permutations, N-Queens puzzle 分类: hdoj 2015-07-19 16:49 86人阅读 评论(0) 收藏
one recursive approach to solve hdu 1016, list all permutations, solve N-Queens puzzle. reference: t ...
- leetcode N-Queens/N-Queens II, backtracking, hdu 2553 count N-Queens, dfs 分类: leetcode hdoj 2015-07-09 02:07 102人阅读 评论(0) 收藏
for the backtracking part, thanks to the video of stanford cs106b lecture 10 by Julie Zelenski for t ...
随机推荐
- [xampp]在Crunch Bang下安装xampp1.8.3
1.下载linux下 的xampp安装包 xampp-linux-1.8.3-5-installer.run 2.终端下, 给执行权限 sudo chmod +x ./xampp-linux-1.8. ...
- js asp.net获取时间戳
方法1: var timestamp = (new Date()).valueOf(); 结果:1280977330748 方法2: var timestamp=new Date().getTime( ...
- 在页面关闭或者刷新的时候触发 onbeforeunload
该实例演示了如何使用 HTML DOM 向 body 元素添加 "onbeforeunload" 事件. 关闭当前窗口,按下 F5 或点击以下链接触发 onbeforeunload ...
- python-操作excel数据文件
1.excel文件操作 读文件xlrd模块:
- __block 与 __weak的区别理解
Blocks理解: Blocks可以访问局部变量,但是不能修改 如果修改局部变量,需要加__block __block int multiplier = 7; int (^myBlock)(int) ...
- O2O管理问题怎么解决?
O2O被认为是零售行业的最大变革.O2O模式凭着线上线下一体化融合,无差别的消费体验迅速拉拢了大批希望进军电商的传统零售企业,同时催生了大量以O2O为生的新兴企业.O2O是一种新的消费模式,给消费者带 ...
- mysql之show engine innodb status解读
注:以下内容为根据<高性能mysql第三版>和<mysql技术内幕innodb存储引擎>的innodb status部分的个人理解,如果有错误,还望指正!! innodb存 ...
- 关于在官网上查看和下载特定版本的webrtc代码
注:这个方法已经不适用了,帖子没删只是留个纪念而已 gclient:如果不知道gclient是什么东西 ... 就别再往下看了. 下载特定版本的代码: #gclient sync --revision ...
- JS去掉数组的重复项
自己知道思路怎么去,但是就是自己不会写,在网上找了一些来看,有些还是没有怎么看明白.学习到了这么一种方法 var a=['ss','dd','ss','cc','dd',1,2,1] var b={} ...
- [poj3017] Cut the Sequence (DP + 单调队列优化 + 平衡树优化)
DP + 单调队列优化 + 平衡树 好题 Description Given an integer sequence { an } of length N, you are to cut the se ...