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. 解决启动Eclipse后提示’Running android lint’错误的问题

    打开项目的AndroidManifest.xml文件,android:targetSdkVersion="21"改为“20”或以下的值.由于Android L为预览版本,版本号还是 ...

  2. 解决jQuery ajax跨域问题,Google、IE、Firefox亲测有效

    直接上最后的结果吧 JS: $.ajax({ type: "GET", async: false, crossDomain: true, url: "www.test.c ...

  3. SQL2008根据日志恢复

    --创建测试数据库 CREATE DATABASE Db GO --对数据库进行备份 BACKUP DATABASE Db TO DISK='c:\db.bak' WITH FORMAT GO --创 ...

  4. 谈谈JDK线程的伪唤醒

    在JDK的官方的wait()方法的注释中明确表示线程可能被"虚假唤醒",JDK也明确推荐使用while来判断状态信息.那么这种情况的发生的可能性有多大呢? 使用生产者消费者模型来说 ...

  5. 图片上传本地预览。兼容IE7+

    基于JQUERY扩展,图片上传预览插件 目前兼容浏览器(IE 谷歌 火狐) 不支持safari 预览地址:http://www.jinbanmen.com/test/1.html js代码:/**名称 ...

  6. Google 面试题和详解

    Google的面试题在刁钻古怪方面相当出名,甚至已经有些被神化的味道.这个话题已经探讨过很多次,而科技博客 BusinessInsider这两天先是贴出15道Google面试题并一一给出了答案,其中不 ...

  7. windows 中 使用MongoDB

    MongoDB简介 MongoDB是一个高性能,开源,无模式的文档型数据库,是当前NoSql数据库中比较热门的一种.它在许多场景下可用于替代传统的关系型数据库或键/值存储方式. 传统的关系数据库一般由 ...

  8. Maven学习(一) -- 安装Maven及Eclipse中配置Maven

    标签(空格分隔): 学习笔记 本文环境:Windows7, JDK1.7.0_76 安装及配置Maven环境变量 需要电脑中已经有Java环境 在控制台中输入:echo %JAVA_HOME%看是否能 ...

  9. 正则表达式获取TABLE里的内容

    //过滤\n 转换成空            String withoutNString=message.Replace("\n", "");          ...

  10. swig之于c++

    [namespace] namespace nsTest1 { int nsAdd(int a, int b) { return a + b; } } namespace nsTest2 { int ...