返回   cpper编程论坛 > 算法
注册账号 论坛帮助 会员列表 日历事件 搜索 今日新帖 标记版面已读

回复
 
LinkBack 主题工具 显示模式
  #1 (permalink)  
旧 2006-09-07
liuxinyu 的头像
高级会员
 
注册日期: 2006-02-09
帖子: 303
文章: 48
liuxinyu 正向着好的方向发展
默认 Lucky Number

> Write a program that tells whether a given integer is happy. A
> happy number is
> found using the following process: Take the sum of the squares of
> its digits,
> and continue iterating this process until it yields 1, or produces
> an infinite
> loop.
>
> For example the number 7:
>
> 7^2 = 49
> 4^2 + 9^2 = 97
> 9^2 + 7^2 = 130
> 1^2 + 3^2 + 0^2 = 10
> 1^2 + 0^2 = 1
>
> If a number is not happy than it is obviously unhappy. Now that you
> have this
> program, what is the largest happy number you can find? What is the
> happiest
> number between 1 and 1,000,000. I define the happiest number as the
> smallest
> number that finds the most other happy numbers with it, i.e. 7
> found four other
> numbers (49, 97, 130, and 10) making it a rank 4 in happiness.
>
> If you find all these examples trivial, write you program so that
> it will find
> happy numbers in other bases such as base 2 or 16. From there you
> can extend the
> program so that it finds happy bases (other than 2 and 4). A happy
> bases is a
> base where all numbers are happy. Good luck.
>
__________________
==================================
http://liuxinyu95.googlepages.com
liuxinyu95@gmail.com
==================================
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #2 (permalink)  
旧 2008-07-01
liuxinyu 的头像
高级会员
 
注册日期: 2006-02-09
帖子: 303
文章: 48
liuxinyu 正向着好的方向发展
默认 回复: Lucky Number

正好有人在toplanguage上贴了另一种lucky number:
对于一个正整数N,定义magic(N)为由N的各相邻位两个数字之差组成的正整数,如:magic(5913)=482,
magic(119=081=81,
magic(666)=00=0。对N反复求magic直至得到一个一位数的数字,该数字称为N的magic指纹,如对于5913,依次计算magic得到:482,46,2。我们称5913的magic指纹为2。我们把magic指纹为7的数称为幸运数。给定两个正整数N,编程计算出在1至N中幸运数的数量。如:
1-10中有1个:7;
1-100中有6个:7,18,29,70,81,92;
1-1000中有28个:7,18,...,980,992;

除了穷举以外,我没有想到更好的方法。但题目要求N的范围到10^9,并且要在2秒内完成运算,穷举肯定不能满足速度的要求。大家有什么好的办法?

我给一个程序:
代码:
#include <iostream> #include <list> using namespace std; typedef list<int> integer; bool is_valid(int y){ return 0<=y && y<=9; } list<integer> cons(int x, list<integer> xs){ for(list<integer>::iterator it=xs.begin(); it!=xs.end(); ++it) (*it).push_front(x); return xs; } integer cons(int x1, int x2){ integer x; x.push_front(x2); x.push_front(x1); return x; } list<integer> append(list<integer> xs, list<integer> ys){ for(list<integer>::iterator it=ys.begin(); it!=ys.end(); ++it) xs.push_front(*it); return xs; } list<integer> next_digit(integer ds, int y){ list<integer> res; int d=ds.front(); integer xs=cons(y+d, y-d); for(integer::iterator it=xs.begin(); it!=xs.end(); ++it){ int x=*it; if(is_valid(x)){ ds.pop_front(); if(ds.empty()) res.push_back(cons(y,x)); else res=append(res, cons(y, next_digit(ds, x))); ds.push_front(d); } } return res; } list<integer> expand_digit(integer ds){ list<integer> res; for(int x=1; x<=9; ++x) res=append(res, next_digit(ds, x)); return res; } void print_integer(integer x){ for(integer::iterator it=x.begin(); it!=x.end(); ++it) cout<<*it; } void print_integers(list<integer> xs){ for(list<integer>::iterator it=xs.begin(); it!=xs.end(); ++it){ print_integer(*it); cout<<", "; } } void search(list<integer> xs, int digit){ list<integer> res; for(list<integer>::iterator it=xs.begin(); it!=xs.end(); ++it){ res=append(res, expand_digit(*it)); print_integers(res); } if(digit-1) search(res, digit-1); } int main(int, char**){ integer x0; x0.push_back(7); list<integer> res; res.push_back(x0); cout<<"max digit of the lucy number:"; int i; cin>>i; search(res, i-1); }
运行结果如下:
@liu-sharp:~/work/cpp$ g++ luckyno.cpp -o luckyno
liu@liu-sharp:~/work/cpp$ ./luckyno
max digit of the lucy number:3
18, 29, 70, 81, 92, 108, 219, 780, 891, 980, 209, 790, 108, 219, 780, 891, 980, 188, 299, 700, 811, 922, 209, 790, 108, 219, 780, 891, 980, 198, 801, 912, 910, 188, 299, 700, 811, 922, 209, 790, 108, 219, 780, 891, 980, 902, 198, 801, 912, 910, 188, 299, 700, 811, 922, 209, 790, 108, 219, 780, 891, 980,
__________________
==================================
http://liuxinyu95.googlepages.com
liuxinyu95@gmail.com
==================================

此帖于 2008-07-01 12:29 PM 被 liuxinyu 编辑.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #3 (permalink)  
旧 2008-07-01
liuxinyu 的头像
高级会员
 
注册日期: 2006-02-09
帖子: 303
文章: 48
liuxinyu 正向着好的方向发展
默认 回复: Lucky Number

拿Haskell缩水一下:
代码:
lucky [] x0 = [[]] lucky (d:ds) x0 = [x1:xs | x1<-[x0+d, x0-d], xs<-(lucky ds x1), (x1>=0 && x1<=9)] lucky_numbers ds = [ x1:xs | x1<-[1..9], xs<-lucky ds x1]
运行时:
Lucky> lucky_numbers [7]
[[1,8],[2,9],[7,0],[8,1],[9,2]]
map lucky_numbers (lucky_numbers [7])
[[[1,0,8],[2,1,9],[7,8,0],[8,9,1],[9,8,0]],[[2,0,9],[7,9,0]],[[1,8,8],[1,8,8],[2,9,9],[2,9,9],[7,0,0],[7,0,0],[8,1,1],[8,1,1],[9,2,2],[9,2,2]],[[1,9,8],[8,0,1],[9,1,2],[9,1,0]],[[9,0,2]]]
有bug,待查...

此帖于 2008-07-01 09:36 PM 被 liuxinyu 编辑.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
回复

书签

主题工具
显示模式

发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛启用 表情符号
论坛启用 [IMG] 代码
论坛禁用 HTML 代码
Trackbacks are 启用
Pingbacks are 启用
Refbacks are 启用


相似的主题
主题 主题作者 版面 回复 最后发表
[TGA]图像文件格式 polyrandom 技术杂烩 0 2004-07-01 10:02 PM
[GIF]图像文件格式 polyrandom 技术杂烩 0 2004-07-01 09:50 PM
[ZIP]ZIP文件格式 polyrandom 技术杂烩 0 2004-07-01 09:48 PM
[BMP]文件格式 codinggirl 技术杂烩 3 2004-07-01 09:40 PM
[XLS]excel格式 famel 技术杂烩 1 2004-07-01 08:58 PM


所有时间均为格林尼治时间 +9。现在的时间是 10:05 PM


Powered by vBulletin® 版本 3.7.0
版权所有 ©2000 - 2008,Jelsoft Enterprises Ltd.
(C) Copy Right All Right Reserved 2001 - 2007

Search Engine Friendly URLs by vBSEO 3.1.0