JonsenElizee

Software Developing Blog

"An idea is fragile . It can be killed by a scornful smile or a yawn .It can be mound down by irony and scared to death by a cold look."
"Most cultures throughout human history have not liked creative individuals .They ignore them or kill them.It is a very efficient way of stopping creativity."

------Advertising boss Charles Browe and Howard Gardner ,professor at Harvard

   :: 首页 :: 新随笔 ::  ::  :: 管理 ::
Here is a simple implementation for this issue. And it's TC is O(n/10), O(n);
Any better one is expected.
 1 int onenumber(int n)
 2 {
 3     if(n <= 0return 0;
 4     if(n <= 9return 1;
 5 
 6     int bak = n, hig = 1, num = 0;
 7     while(n >= 10) {
 8         hig *= 10;
 9         n /= 10;
10     }
11 
12     if(n == 1) {
13         num = bak-hig+1 + ((bak-hig == 0? 0 : (bak-hig <= 9 ? 1 : onenumber(bak-hig)));
14         num += (hig-1 <= 9 ? 1 : onenumber(hig-1));
15     }
16     else {
17         num = (bak - n*hig == 0 ? 0 : (bak - n*hig <= 9 ? 1 : onenumber(bak - n*hig)));
18         num += (n*hig-1 <= 9 ? 1 : onenumber(n*hig-1));
19     }
20 
21     return num;
22 }

posted on 2010-10-24 19:04 JonsenElizee 阅读(1282) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


By JonsenElizee