题目描述:

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.

Example: 19 is a happy number12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1

  以前一直是按时间顺序做的,从这题开始按难易程度做。

我的解法:

bool isHappy(int n) {if(n <= 0)return false;set<int> st;while (1){int tmp = 0;while (n > 0){tmp += (n % 10)*(n % 10);n /= 10;}if(tmp == 1)return true;else{if(st.find(tmp) != st.end())return false;elsest.insert(tmp);}n = tmp;}
}

简洁版:

bool isHappy(int n) {if(n <= 0)return false;unordered_set<int> st;while (n != 1 && st.find(n) != st.end()){st.insert(n);int tmp = 0;do {tmp += (n % 10) * (n % 10);} while ( (n /= 10) > 0);n = tmp;}return n == 1;
}

转载于:https://www.cnblogs.com/gattaca/p/4594675.html

Happy Number相关推荐

  1. input属性为number,maxlength不起作用的解决方案

    <input type="text" maxlength="11" /> 效果ok, 当 <input type="number&q ...

  2. ORA-19502: write error on file xxxxx, block number xxxx

    错误现象: 在ORACLE 10g下为表空间IGNITE_EGVSQL01增加数据文件时,报如下错误: SQL> ALTER TABLESPACE IGNITE_EGVSQL01      AD ...

  3. LeetCode刷题记录10——434. Number of Segments in a String(easy)

    LeetCode刷题记录10--434. Number of Segments in a String(easy) 目录 LeetCode刷题记录9--434. Number of Segments ...

  4. HDU 1711 Number Sequence(KMP算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/ ...

  5. LeetCode 191 Number of 1 Bits

    LeetCode 191 Number of 1 Bits 解法一(较为传统都解法):使用将n不断右移,并与1想&得到1的个数:(也有使用除法/2的,明显除法的运行效率要低于位移) 时间复杂度 ...

  6. [BuildRelease]build number / id

    build number, 也称为build id, 在build release的流程中唯一标示一个build,也是正式的产品的product version 和file version后两位(Ma ...

  7. Oracle-ORA-01722 invalid number错误

    本来正常的,经过抓包才知道原来是数字型的无意中多了乱码! 本来是3276的居然多了后面一串 3276PuAnrSeU2zliU+IV/FHlnX2Xgia1au2xX2vMWtw http://www ...

  8. input输入框为number类型时,去掉上下小箭头

    input输入框type为number时,去掉上下小箭头,方式如下: <input type="number" ...><style>/* 在Chrome浏 ...

  9. 表单系列之input number总结

    各浏览器表现 <input type="number" /> chrome 除数字字符,只可输入e和. IE 除数字字符,其他字符均可输入,无报错 Firefox 除数 ...

  10. 【HDU】1005 Number Sequence (有点可爱)

    http://acm.hdu.edu.cn/showproblem.php?pid=1005 A number sequence is defined as follows: f(1) = 1, f( ...

最新文章

  1. 信科c语言实验程序修改题_豆瓣评分 9.3,史上最好的 C 语言著作,竟然翻车了.........
  2. XAML数据绑定(Data Binding)
  3. PostgreSQL在何处处理 sql查询之六十六
  4. 分布式计算 MapReduce与yarn工作机制
  5. arduinojson 转 string_安德胜工作室发来本周五嗨唱转起来第二季首秀的嘉宾剧透...
  6. 关于MD5破解这件事
  7. 今年Q3发布!小米12至尊版曝光:升级骁龙8 Plus处理器
  8. html 多页面合并,让多个HTML页面 使用 同一段HTML代码
  9. STL的Deque介绍
  10. 49个Python学习资源:从初学者到高级玩家都有了
  11. python logging设置_Django logging配置及使用详解
  12. Fix: Send-Mail: Warning: Inet_protocols: IPv6 Support Is Disabled
  13. TI DSP 28335 自学之路,到此止步
  14. GNS3模拟思科三层交换机(排错细节)
  15. 第二十五届全国青少年信息学奥林匹克联赛初赛
  16. Oracle 12c 创建新用户报错 - ORA-65096
  17. The MegaFace Benchmark-1 Million Faces for Recognition at Scale
  18. Centos7编译安装Xen环境(vtpm)
  19. 墨客开发者行动(上海站)
  20. 英语语法---表语详解

热门文章

  1. 算法---------路径总和
  2. android 获取短信验证码倒计时
  3. 创建型模式--多例模式
  4. python 事件通知模式_请问在 Python 的事件系统中,如何可以通过事件通知立刻终结一个正在运行的子线程?...
  5. oc之mac开发 - NSButton - 勾选框(NSButtonTypeSwitch)
  6. Python中字符串的截取,列表的截取
  7. IMAP IDLE模式(推送邮件)
  8. 关于Linux下进程创建的相关知识
  9. static_cast, dynamic_cast, const_cast
  10. html javascript 引号