happy num
来源:互联网 发布:bat转换成exe 知乎 编辑:程序博客网 时间:2024/05/29 15:05
例如
Example: 19 is a happy number
- 12 + 92 = 82
- 82 + 22 = 68
- 62 + 82 = 100
- 12 + 02 + 02 = 1
计算出现重复,则不是
class Solution {
public:unordered_map<int ,int >result;
bool isHappy(int n) {
/*
if (n==1)
{
return true;
}
else{
unordered_map<int ,int>result_count;
int t=0;
++result_count[1];
while(1){
while(n)
{
t=t+(n%10)*(n%10);
n=n/10;
}
if(t==1)
return true;
if(result_count.find(t)!=result_count.end())
{
return false;
}
++result_count[t];
n=t;
t=0;
}
}
*/
if(n==1)
return true;
int t=0;
while(n)
{
t=t+(n%10)*(n%10);
n=n/10;
}
if(result.empty())
{
result[1]++;
}
else
{
if(t==1)
return true;
if(result.find(t)!=result.end())
{
return false;
}
}
++result[t];
return isHappy(t);
}
};
0 0
- happy num
- 从Happy num所想到的几个问题
- happy!
- Happy
- Happy?
- happy
- happy!
- Happy
- happy
- happy
- Happy
- happy
- happy
- Happy
- happy
- happy
- happy!
- Happy!
- 完数 hdoj1406
- 一个简单的wifi开关shell脚本实现
- linux 下常用命令
- android NFC开发学习笔记(2)
- java笔记:javaEE框架(二)--业务层Service以及Service单元测试
- happy num
- Matlab中利用findpeaks找波峰和波谷
- jQuery包装集
- NSDictionary (字典API+排序)
- PHP count()利用数组元素个数的不相等来判断是否是一维数组?
- poj 1283 Moving Computer dp解小球放小盒
- Log4j 配置和使用
- java web 学习-filter简介
- 51nod 1449 砝码称重 (二进制思想)