#leetcode#202. Happy Number
2016-04-25 21:33
337 查看
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
构成群
4
bool isHappy(int n) {
if (n <= 0) return false;
int magic = 4;
while (1) {
if (n == 1) return true;
if (n == magic) return false;
int t = 0;
while (n) {
t += (n % 10) * (n % 10);
n /= 10;
}
n = t;
}
}
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
构成群
4
bool isHappy(int n) {
if (n <= 0) return false;
int magic = 4;
while (1) {
if (n == 1) return true;
if (n == magic) return false;
int t = 0;
while (n) {
t += (n % 10) * (n % 10);
n /= 10;
}
n = t;
}
}
相关文章推荐
- 应用session和application对象设计聊天室
- Android开发之Bitmap二次采样
- Android开发之Bitmap二次采样
- Android开发之Bitmap二次采样
- Xilinx中的xapp1052理解
- android library projects cannot be launched
- Android开发之Bitmap二次采样
- 用android LinearLayout和RelativeLayout实现精确布局
- APP UI Design ——sketch(二)
- ios中isa指针
- iOS调试证书制作。。。
- android AsyncTask介绍
- cin/cou效率太慢?试一下 ios::sync_with_stdio(false);
- 三、原型设计模式
- Android系统自带样式(android:theme)
- Android Studio常用快捷键---不断更新中
- 微信公众号授权问题总结
- 实现带标题的ListView
- Android %1$s %1$d记载
- android手机获取本地ip为10.2.0.5 的解决方法