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) 收藏的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 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) ...

  6. 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) ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 曲线提取数据Engauge Digitizer

    可导出CSV格式数据 其它参考: http://blog.sina.com.cn/s/blog_4ae65b4d0100z8cg.html 其它曲线提取数据的软件还有: GetData.Windig ...

  2. InnoDB Spin rounds per wait在>32位机器上可能为负

    今天发现一个系统innodb的spin rounds per wait为负,感觉很奇怪,原来是个bug: For example (output from PS but we have no patc ...

  3. linux 互信不生效

    版权声明:本文为博主原创文章,未经博主允许不得转载. 1.  操作系统版本 1)操作系统 cat /etc/issue cat /etc/issue CentOS release 6.6 (Final ...

  4. Cookie与Session的区别

    cookie机制 Cookies是服务器在本地机器上存储的小段文本并随每一个请求发送至同一个服务器.IETF RFC 2965 HTTP State Management Mechanism 是通用c ...

  5. Cheatsheet: 2015 08.01 ~ 08.31

    Java Beginner's Guide to MVC with Spring Framework Exploring the Spring Web MVC for Web Application ...

  6. GZFramwork数据库层《前言》DLL项目引用

    新建项目: 1. 项目引入GZFramwork.dll NuGet地址:Install-Package GZFramwork 每个项目都引用 2.BLL层 设置数据库连接维护类:继承于:GZFramw ...

  7. 在代码中调用gdb

    转载:http://blog.csdn.net/hanchaoman/article/details/5583457 在SIGSEGV的handler中调用gdb, 命令gdb [exec file] ...

  8. Android Activity的加载模式和onActivityResult方法之间的冲突

    前言 今天在调试程序时,发现在某一Activity上点击返回键会调用该Activity的onActivityResult()方法.我一开始用log,后来用断点跟踪调试半天,还是百思不得其解.因为之前其 ...

  9. osg渲染数据高程文件

    使用gdal解析DEM文件,将高程数据转换为HeightField对象,然后在osg渲染. 1 源代码 #include <gdal_priv.h> #include <osgVie ...

  10. PHP 小方法之 写日志方法

    if(! function_exists ('write_log') ) { function write_log($data, $name='debug', $date=null){ if (is_ ...