问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3856 访问。

编写一个算法来判断一个数是不是“快乐数”。

一个“快乐数”定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,也可能是无限循环但始终变不到 1。如果可以变为 1,那么这个数就是快乐数。

输入: 19

输出: true

解释: 





Write an algorithm to determine if a number is "happy".

A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.

Input: 19

Output: true

Explanation: 





示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3856 访问。

public class Program {

    public static void Main(string[] args) {
int nums = 19; var res = IsHappy(nums);
Console.WriteLine(res); nums = 26;
res = IsHappy2(nums);
Console.WriteLine(res); nums = 65;
res = IsHappy3(nums);
Console.WriteLine(res); Console.ReadKey();
} private static bool IsHappy(int n) {
//递归法
var set = new HashSet<int>();
return ComputerHappy(n, set);
} private static bool ComputerHappy(int n, HashSet<int> set) {
if(n == 1) return true;
if(set.Contains(n)) return false;
set.Add(n);
//计算各位数平方和
//int sum = 0;
//for(int i = 0; i < n.ToString().Length; i++) {
// sum += (int)Math.Pow(n / (int)Math.Pow(10, i) % 10, 2);
//}
return ComputerHappy(SquareSum(n), set);
} private static int SquareSum(int n) {
//计算各位数平方和
var sum = 0;
while(n > 0) {
sum += (n % 10) * (n % 10);
n = n / 10;
}
return sum;
} private static bool IsHappy2(int n) {
//若一个数不是快乐数,其必定死在4手上,最终进入以下死循环
//4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4
//4的各数位平方是16,将y进行2次各位数平方和计算
//若相等,则到达了4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4
//这样的死循环或到达了1这样的快乐数结束值
//返回x == 1即可判定是不是快乐数
var x = n;
var y = n;
do {
x = SquareSum(x);
y = SquareSum(SquareSum(y));
} while(x != y);
return x == 1;
} private static bool IsHappy3(int n) {
//参考IsHappy2
while(n != 1 && n != 4) {
var sum = 0;
while(n > 0) {
sum += (n % 10) * (n % 10);
n /= 10;
}
n = sum;
}
return n == 1;
} }

以上给出3种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3856 访问。

True
False
False

分析:

该题的时间复杂度分析依赖于一些数学知识,若有看官知道如何分析请给我留言,不胜感激!

最新文章

  1. 微信小程序常见错误及基本排除方法
  2. BZOJ 1770: [Usaco2009 Nov]lights 燈
  3. java面试题个人总结
  4. SQL 高效分页查询
  5. 使用gulp添加版本号
  6. 怎么创建MongoDB数据库
  7. 教你记住ASP.NET WebForm页面的生命周期
  8. 【COGS1672】难存的情缘
  9. andoid x项目的优化 1
  10. spool命令、创建一个表,创建而且copy表,查看别的用户下的表,rowid行地址 索引的时候使用,表的增删改查,删除表,oracle的回收站
  11. Head First设计模式之组合模式
  12. [原创]自动获取当前URL所属主域的JS方法(适合多级域名)
  13. Word 测试下发布博客
  14. Py之set操作【转载】
  15. g++编译后中文显示乱码解决方案(c++)
  16. ubuntu16.04 彻底卸载MySQL
  17. 第一次课堂作业之Circle
  18. std::string compare
  19. redis之(七)redis的集合类型的命令
  20. KDJ金叉测试

热门文章

  1. 理解Spring(一):Spring 与 IoC
  2. OSCP Learning Notes - WebApp Exploitation(5)
  3. 事件循环 event loop 究竟是什么
  4. 写给.NET开发者的Python教程(二):基本类型和变量
  5. swagger -- 前后端分离的API接口
  6. 数据库-SQL查询语言(一)
  7. Java应用服务器之tomcat会话复制集群配置
  8. html头文件设置常用之&lt;meta&gt;设置
  9. NACOS安装和配置
  10. PHP array_uintersect() 函数