least calculation time to get 1, if n is odd,add or sub 1,if even divide by one.

iteration solution:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <iostream>
using namespace std;
int function(unsigned int n)
{
if( n == 1 ) return 0;
if( (n % 2) == 0) return 1 + function(n/2);
return 2 + min(function(n-1)/2, function(n+1)/2);
}
int main()
{
for(int i = 1; i < 1000; i++)
{
printf("%d\n",function(i));
}
system("pause");
return 1;
}

posted on 2012-08-18 08:57 三少_爷 阅读(125) 评论(0)  编辑 收藏 引用


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


<2012年8月>
2930311234
567891011
12131415161718
19202122232425
2627282930311
2345678

导航

统计

常用链接

留言簿

随笔分类

随笔档案

My Website

搜索

最新评论

阅读排行榜

评论排行榜