【LeetCode】(202)Happy Number(Easy)
2015-08-21 19:34
441 查看
题目
Happy Number
Total Accepted: 26154 TotalSubmissions: 82078My Submissions
Question
Solution
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 number
12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 =
1
解析
循环判断就好了,直接上代码class Solution { public: bool isHappy(int n) { int m = 0; while (n!=0) { m += (n%10)*(n%10); n = n/10; } if (m == 1) { return true; } else if (m<9) { return false; } else return isHappy(m); } };
相关文章推荐
- 微信内置浏览器的 User Agent的判断
- 关于UIScreen的applicationFrame和bounds方法区别
- ios开发学习--歌词处理--解析lrc文件
- iOS将数组中的内容分拼接成字符串
- 如何学习iOS开发?iOS Developer Library足矣!
- Android 时间格式化符号HH无法识别
- Android常见的几个控件(TextView,EditText,Button)
- android 组件 ProgressDialog实例
- android bitmap的放大缩小
- android 组件GridView实例
- Android Fragment(一)---简介
- swift 自定义TextField 的边框颜色和 左右 视图
- 读Effective Objective C总结(二)
- IOS开发之动态获取模型的属性值
- android 组件SeekBar实例
- iOS 屏幕适配
- Android中实现垂直滑动中水平滑动毫无障碍
- android 组件ProgressBar实例
- 微信公众平台开发(2)回复超链接
- 微信公众平台开发(2)回复超链接