leetcode208 happynumber
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的更多相关文章
- [Swift]LeetCode208. 实现 Trie (前缀树) | Implement Trie (Prefix Tree)
Implement a trie with insert, search, and startsWith methods. Example: Trie trie = new Trie(); trie. ...
- leetcode208
class TrieNode { public: // Initialize your data structure here. TrieNode() { words=; prefixs=; ;i&l ...
- LeetCode208:Implement Trie (Prefix Tree)
Implement a trie with insert, search, and startsWith methods. Note: You may assume that all inputs a ...
- Leetcode208. Implement Trie (Prefix Tree)实现Trie(前缀树)
实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作. 示例: Trie trie = new Trie(); trie.insert(" ...
- 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 ...
- [LeetCode] Happy Number 快乐数
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- leetcode-【简单题】Happy Number
题目: Write an algorithm to determine if a number is "happy". A happy number is a number def ...
- Happy Number
https://leetcode.com/problems/happy-number/ Write an algorithm to determine if a number is "hap ...
- LeetCode 解题报告--202Happy Number
1 题目描述 Write an algorithm to determine if a number is "happy". A happy number is a number ...
随机推荐
- Nancy之实现API
Nancy之实现API的功能 0x01.前言 现阶段,用来实现API的可能大部分用的是ASP.NET Web API或者是ASP.NET MVC,毕竟是微软官方出产的,用的人也多. 但是呢,Nancy ...
- android 中获取网络状态、判断3G、2G、wifi网络、判断wifi是否打开、获取本机地址、获取本机串号IMEI整理
代码如下:package com.android.xym; import java.io.IOException; import java.net.HttpURLConnection; import ...
- listview 的onitemlongclick阿和onitemclick冲突,item中还有button的点击事件
listview里面item有button的,button要设置 android:focusable="false" ,listview里面如果设置了 onitemlongcli ...
- CCNA实验(9) -- Frame Relay
帧中继的一些特点:1.中小企业常用的广域网线路2.通信费用较低3.配置较为复杂 1.将Cisco路由器配置为帧中继交换机2.帧中继基本配置.帧中继映射3.在帧中继的链路上运行RIPv24.帧中继的多点 ...
- BZOJ 2002 [Hnoi2010]Bounce 弹飞绵羊(动态树)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2002 [题目大意] 给出一片森林,操作允许更改一个节点的父亲,查询一个节点的深度. [ ...
- js 计算两个时间差
/* * 计算两个日期的间隔天数* BeginDate:起始日期的文本框,格式為:2012-01-01* EndDate:結束日期的文本框,格式為:2012-01-02* 返回兩個日期所差的天數* 調 ...
- 【iOS】Mapkit的使用:地图显示、定位、大头针、气泡等
转自:http://blog.csdn.net/dolacmeng/article/details/46594839 以前做项目用高德地图SDK,需要注册账号和AppID,然后下载SDK集成到项目中, ...
- 空类的默认函数—— SAP电面(2)/FEI
定义一个空类 class Empty { }; 默认会生成以下几个函数 2. 拷贝构造函数 Empty(const Empty& copy) { } 3. 赋值运算符 Empty& o ...
- C++对象模型3--无重写的单继承
C++对象模型中加入单继承 不管是单继承.多继承,还是虚继承,如果基于“简单对象模型”,每一个基类都可以被派生类中的一个slot指出,该slot内包含基类对象的地址.这个机制的主要缺点是,因为间接性而 ...
- Lucence.net索引技术 二
一. Lucene索引创建和优化 [版本2.9.0以上] Lucene索引的创建首先需要取得几个必须的对象: 1.分词器//可以采用其他的中文分词器 StandardAnalyzer analyzer ...