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. Yii2.0中文开发向导——Where条件查询全解析

    在Yii的Model里进行查询的时候 where是必不可少的.Where方法声明为 static where( $condition ) 其中参数 $condition类型为字符串或者数组 1.字符串 ...

  2. ms

    meanShift的概念最早是由Fukunage[1]在1975年提出的,其最初的含义正如其名:偏移的均值向量:但随着理论的发展,meanShift的含义已经发生了很多变化.如今,我们说的meanSh ...

  3. WITH AS的含义

    一.WITH AS的含义WITH AS短语,也叫做子查询部分(subquery factoring),可以让你做很多事情,定义一个SQL片断,该SQL片断会被整个SQL语句所用到.有的时候,是为了让S ...

  4. vs extension

    优先级高低

  5. Openstack命令行创建不同vlan段虚拟机

    默认使用nova-network的vlan模式,但是在使用默认的dashboard的时候,不能指定创建的虚拟机的使用网段,固定IP地址. 实际上该功能是在存在的,只是openstack的dashbbo ...

  6. Linux 奇技淫巧

    为了整理这些命令,花了我一个晚上的时间,但是不弄明白,我就是不爽啊. 1.cmatrix 命令 黑客帝国,就是酷炫,先按F11全屏效果更佳 安装:luffy@ubuntu:~$ sudo apt-ge ...

  7. [课程设计]Scrum 2.6 多鱼点餐系统开发进度(下单一览页面-菜式添加功能实现)

    Scrum 2.6 多鱼点餐系统开发进度  (下单一览页面-菜式添加功能实现) 1.团队名称:重案组 2.团队目标:长期经营,积累客户充分准备,伺机而行 3.团队口号:矢志不渝,追求完美 4.团队选题 ...

  8. Codeforce 567D

    One-Dimensional Battle Ships time limit per test 1 second memory limit per test 256 megabytes input ...

  9. IDispatch error #3092

    在采用ADO访问ACCESS数据库的时候,出现IDispatch error #3092错误的原因之一是在SQL语句中使用了保留关键字.比如:如果表的名称为User 则会出现该错误.,若字段为valu ...

  10. RPM安装rabbitMQ

    系统使用的是centos 7 - minimal 建立用户和组: # groupadd rabbitmq # useradd rabbitmq -g rabbitmq 在安装rabbitMQ之前需要先 ...