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 sum=0;//每次循环的和
bool hasAppear[810];//最多可能出现数的个数
memset(hasAppear, 0, sizeof(hasAppear));//将该数组初始化为0
do{
sum=0;//每次计算和之前
while(n){
int t=n%10;
sum=sum+t*t;
n=n/10;
}
n=sum;
if(sum==1) {return 1;break;}
else if(hasAppear[sum]==1){return 0;break;}
hasAppear[sum]=1;
}while(sum);
}
};

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

int main(){
int n;
cin>>n;
int sum=0;
bool hasAppear[10000];
memset(hasAppear, 0, sizeof(hasAppear));
do{
sum=0;
while(n){
int t=n%10;
sum=sum+t*t;
n=n/10;

}
cout<<"sum="<<sum<<endl;
n=sum;
cout<<"n="<<n<<endl;
if(sum==1) {cout<<"happy"<<endl;return 1;break;}
else if(hasAppear[sum]==1){cout<<"unhappy"<<endl;return 0;break;}
hasAppear[sum]=1;
cout<<"hasAppear["<<sum<<"]="<<hasAppear[sum]<<endl;
}while(sum);
}

leetcode208 happynumber的更多相关文章

  1. [Swift]LeetCode208. 实现 Trie (前缀树) | Implement Trie (Prefix Tree)

    Implement a trie with insert, search, and startsWith methods. Example: Trie trie = new Trie(); trie. ...

  2. leetcode208

    class TrieNode { public: // Initialize your data structure here. TrieNode() { words=; prefixs=; ;i&l ...

  3. LeetCode208:Implement Trie (Prefix Tree)

    Implement a trie with insert, search, and startsWith methods. Note: You may assume that all inputs a ...

  4. Leetcode208. Implement Trie (Prefix Tree)实现Trie(前缀树)

    实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作. 示例: Trie trie = new Trie(); trie.insert(" ...

  5. LeetCode208 Implement Trie (Prefix Tree). LeetCode211 Add and Search Word - Data structure design

    字典树(Trie树相关) 208. Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith  ...

  6. [LeetCode] Happy Number 快乐数

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

  7. leetcode-【简单题】Happy Number

    题目: Write an algorithm to determine if a number is "happy". A happy number is a number def ...

  8. Happy Number

    https://leetcode.com/problems/happy-number/ Write an algorithm to determine if a number is "hap ...

  9. LeetCode 解题报告--202Happy Number

    1 题目描述 Write an algorithm to determine if a number is "happy". A happy number is a number ...

随机推荐

  1. 谷歌日志库GLog 使用说明

    1 引用头文件 加载库 #include <glog/include/logging.h> #pragma comment(lib,"libglog.lib") 2 初 ...

  2. Windows 系统消息范围和前缀,以及消息大全

    Windows系统定义的消息类别消息标识符前缀 消息分类ABM 应用桌面工具栏消息BM 按钮控件消息CB 组合框控件消息CBEM 扩展组合框控件消息CDM 通用对话框消息DBT 设备消息DL 拖曳列表 ...

  3. C++使用OLE高速读写EXCEL的源码

    我的代码参考的地方是这儿,再次感谢原作者 http://blog.csdn.net/gyssoft/archive/2007/04/29/1592104.aspx 我根据自己的需要做了整理,干净了一点 ...

  4. BZOJ 2252: [2010Beijing wc]矩阵距离

    题目 2252: [2010Beijing wc]矩阵距离 Time Limit: 10 Sec  Memory Limit: 256 MB Description 假设我们有矩阵,其元素值非零即1 ...

  5. Google市场推广统计

    Google Play作为Android最大的应用市场,也存在这推广等常用的行为,那么如何统计呢,Google Analytics SDK或者其他的SDK都提供了方法,实际上是可以不需要任何sdk,完 ...

  6. 再学习sqlhelper

    在机房收费重构系统的时候,第一次学习sqlhelper.当时感觉比较简单,没有写博客总结,现在又经过了图书馆的学习,感觉还是有必要写一写的. SqlHelper是一个基于.NETFramework的数 ...

  7. C/C++指针的指针(**p)和指针的引用(*&amp;)使用案例分析

    #include <stdio.h> #include <unistd.h> #include <fcntl.h> #include <stdlib.h> ...

  8. 编写可维护的JS 01

    1.编程风格 缩进层级 使用制表符进行缩进 2个/4个空格缩进 语句结尾 不省略分号 行的长度 不超过80个字符 换行 在运算符后面换行 空行 在以下场景中添加: 方法之间 在方法中局部变量与第一条语 ...

  9. WINFORM Tootip使用小结

    toolTip1.Active = true;   //激活工具提示,只有激活才会显示提示 toolTip1.IsBalloon = true;    //toolTip以气泡形式出现 toolTip ...

  10. spring 加载配置文件的相关配置总结

    PropertyPlaceholderConfigurer      注意: Spring容器仅允许最多定义一个PropertyPlaceholderConfigurer(或<context:p ...