| ||||
| > 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 ================================== |
| ||||
| 正好有人在toplanguage上贴了另一种lucky number: 对于一个正整数N,定义magic(N)为由N的各相邻位两个数字之差组成的正整数,如:magic(5913)=482, magic(119 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秒内完成运算,穷举肯定不能满足速度的要求。大家有什么好的办法? 我给一个程序: 代码:
@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 编辑. |
![]() |
| 书签 |
| 主题工具 | |
| 显示模式 | |
| |
相似的主题 | ||||
| 主题 | 主题作者 | 版面 | 回复 | 最后发表 |
| [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 |