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. Html 小插件10 即时新闻

    效果图 <!--即时新闻--><iframe name="alimamaifrm" frameborder="0" marginheight= ...

  2. oracle dataguard 角色切换

  3. HTML5API___manifest

    离线缓存 manifest 在html标签里面增加个属性 mainfest 就可以告诉浏览器缓存文件在哪里. <html manifest='show.manifest' xmlns=" ...

  4. POJ 2115 模线性方程 ax=b(mod n)

    /* (x*c+a)%(2^k)==b →(x*c)%(2^k)==b-a 满足定理: 推论1:方程ax=b(mod n)对于未知量x有解,当且仅当gcd(a,n) | b. 推论2:方程ax=b(m ...

  5. Linux新手笔记 sudo

    centos 6.4 32bit 你是也像我一样,厌烦了在root用户和个人用户之间来回切换.或者干脆直接用root用户.可以这样设置,然后在命令前加sudo 即可使用自己到密码,临时用root身份执 ...

  6. 解决MySQL 一闪而过的情况

       首先进入cmd 切入MySQL的安装目录,然后切入 bin 目录 ,输入mysqld -n t --skip-grant-tables命令. 这个 cmd 窗口先不要关闭, 打开另一个窗口 登陆 ...

  7. C++ 标准模板库(STL)

    C++ 标准模板库(STL)C++ STL (Standard Template Library标准模板库) 是通用类模板和算法的集合,它提供给程序员一些标准的数据结构的实现如 queues(队列), ...

  8. 最大流之sap算法

    若有向图G = (V , E)满足下列条件: 1.有且仅有一个顶点S,它的入度为 0 ,这个顶点称为源点. 2.有且仅有一个顶点T,它的出度为 0 ,这个顶点称为汇点. 3.每一条弧都有一个非负数,叫 ...

  9. Java学习之equals和==的区别

    转自:http://www.cnblogs.com/zhxhdean/archive/2011/03/25/1995431.html java中的数据类型,可分为两类: 1.基本数据类型  也称原始数 ...

  10. 打印 PHP $_SERVER 常量

    foreach( $_SERVER as $var => $value){ echo $var.' '.$value.'<br>'; };