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 ...
随机推荐
- 跟着8张思维导图学习javascript
学习的道路就是要不断的总结归纳,好记性不如烂笔头,so,下面将po出8张javascript相关的思维导图. 思维导图小tips:思维导图又叫心智图,是表达发射性思维的有效的图形思维工具 ,它简单却又 ...
- JavaScript encodeURI(), decodeURI(), encodeURIComponent(), decodeURIComponent()
URI: Uniform Resource Identifier encodeURI() And decodeURI() The encodeURI() function is used to en ...
- PHPwind高级伪静态规则及方法
phpwind iis下伪静态规则[ISAPI_Rewrite]RewriteRule ^(.*)/(.*)-htm-(.*)-(.*)\.html$ $1/$2\.php\?$3=$4Rewrite ...
- 怎么在php里面利用str_replace防注入
<php /** * 安全过滤函数 * * @param $string * @return string */ function safe_repla ...
- python:HTML + CSS 优先级 返回顶部
优先级 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...
- WPFのInkCanvas作为蒙版透明笔迹不透明
本人最近利用inkcavas做一个蒙版的功能,结果发现笔迹稀释了,经过一番查找发现:应该讲inkcavas的背景设置为白色,然后透明,而不是将整个控件透明,具体代码: <InkCanvas Na ...
- css回忆(一)
1.css的引入方式: a) 在head部分加入<link rel="stylesheet" type="text/css" href="my ...
- ms sql 经典语句【珍藏】
数据库中字段中有不需要"[演示数据请勿真实购买]" 例如: update Hishop_Products set ProductName = replace(ProductName ...
- EXCEL数据透视相关知识
要边看边总结要点:1.部门管理,标准化作业流程,控制生产经营过程,预知风险2.这一项内容,用一个工作薄三个SHEET表来完成.分类汇总表(可变,N个),源数据表(标准.规范.通用.简洁.正确),1.符 ...
- Android 坐标系统
屏幕的左上角是坐标系统原点(0,0),原点向右延伸是X轴正方向,原点向下延伸是Y轴正方向. 一.View的坐标 需要注意view的坐标是相对父容器而言的,包括:getTop().getBott ...