/*
* @lc app=leetcode.cn id=202 lang=c
*
* [202] 快乐数
*
* https://leetcode-cn.com/problems/happy-number/description/
*
* algorithms
* Easy (52.26%)
* Total Accepted: 12.9K
* Total Submissions: 24.7K
* Testcase Example: '19'
*
* 编写一个算法来判断一个数是不是“快乐数”。
*
* 一个“快乐数”定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,也可能是无限循环但始终变不到
* 1。如果可以变为 1,那么这个数就是快乐数。
*
* 示例: 
*
* 输入: 19
* 输出: true
* 解释:
* 1^2 + 9^2 = 82
* 8^2 + 2^2 = 68
* 6^2 + 8^2 = 100
* 1^2 + 0^2 + 0^2 = 1
*
*
*/ int Num(int x)
{
int ret=;
while(x){
ret+=(x%)*(x%);
x/=;
}
return ret;
}
bool isHappy(int n) {
if(n<=)
return false;
while(n!=){
n=Num(n);
if(n==)
return false;
}
return true;
}

这里写了一个函数,专门用来累和的。

然后这里有个小知识,如果无限循环的话,最后结果永远都是4.

-----------------------------------------------------------------------------------

python:

#
# @lc app=leetcode.cn id=202 lang=python3
#
# [202] 快乐数
#
# https://leetcode-cn.com/problems/happy-number/description/
#
# algorithms
# Easy (52.26%)
# Total Accepted: 12.9K
# Total Submissions: 24.7K
# Testcase Example: '19'
#
# 编写一个算法来判断一个数是不是“快乐数”。
#
# 一个“快乐数”定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,也可能是无限循环但始终变不到
# 1。如果可以变为 1,那么这个数就是快乐数。
#
# 示例: 
#
# 输入: 19
# 输出: true
# 解释:
# 1^2 + 9^2 = 82
# 8^2 + 2^2 = 68
# 6^2 + 8^2 = 100
# 1^2 + 0^2 + 0^2 = 1
#
#
#
class Solution(object):
def isHappy(self, n):
# Write your code here
if n is None:
return False
tmp = 0
while tmp != 1 and tmp != 4:
tmp = 0
n = str(n)
for i in n:
tmp += int(i) ** 2
if tmp == 1:
return True
n = tmp
if tmp == 1:
return True
return False

Leecode刷题之旅-C语言/python-202快乐数的更多相关文章

  1. Leecode刷题之旅-C语言/python-9.回文数

    /* * @lc app=leetcode.cn id=9 lang=c * * [9] 回文数 * * https://leetcode-cn.com/problems/palindrome-num ...

  2. Leecode刷题之旅-C语言/python-1.两数之和

    开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...

  3. Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符

    /* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...

  4. Leecode刷题之旅-C语言/python-28.实现strstr()

    /* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/imple ...

  5. Leecode刷题之旅-C语言/python-7.整数反转

    /* * @lc app=leetcode.cn id=7 lang=c * * [7] 整数反转 * * https://leetcode-cn.com/problems/reverse-integ ...

  6. Leecode刷题之旅-C语言/python-434 字符串中的单词数

    /* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...

  7. Leecode刷题之旅-C语言/python-326 3的幂

    /* * @lc app=leetcode.cn id=326 lang=c * * [326] 3的幂 * * https://leetcode-cn.com/problems/power-of-t ...

  8. Leecode刷题之旅-C语言/python-263丑数

    /* * @lc app=leetcode.cn id=263 lang=c * * [263] 丑数 * * https://leetcode-cn.com/problems/ugly-number ...

  9. Leecode刷题之旅-C语言/python-383赎金信

    /* * @lc app=leetcode.cn id=383 lang=c * * [383] 赎金信 * * https://leetcode-cn.com/problems/ransom-not ...

随机推荐

  1. ssh免密码登陆失败的原因

    今天因为需要在两台服务器上进行ssh免登陆,所以安装网上的教程,ssh-keygen -t rsa,然后把相互的密钥加入到对方的authorized_keys. 问题是我们虽然这样做了,却一直要密码, ...

  2. web性能权威指南(High Performance Browser Networking)

    web性能权威指南(High Performance Browser Networking) https://www.cnblogs.com/qcloud1001/p/9663524.html HTT ...

  3. C语言main函数的参数

    在Windows下使用gcc编译器: 1.首先介绍下MinGW MinGW(Minimalist GNU for Windows),又称mingw32,是将GCC编译器和GNU Binutils移植到 ...

  4. MongoDB索引管理

    一.创建索引 创建索引使用db.collectionName.ensureIndex(...)方法进行创建: 语法: >db.COLLECTION_NAME.ensureIndex({KEY:1 ...

  5. Excel 移动列操作

  6. BZOJ2434:[NOI2011]阿狸的打字机(AC自动机,线段树)

    Description 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母. 经阿狸研究发现,这个打字机是这样工作的 ...

  7. VS 2013 代码注释掉,编译不执行问题

    先说说我遇到的问题吧.修改别人的代码,注释原来的代码或者添加新的代码,都不执行,还是编译原来的代码.而且还有一个错误:lc.exe 已退出 代码为 -1 解决方法: 1.先把lc.exe 已退出 代码 ...

  8. HDU 4676 Sum Of Gcd 【莫队 + 欧拉】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=4676 Sum Of Gcd Time Limit: 10000/5000 MS (Java/Others ...

  9. springmvc小结(上)

    1.springmvc的整体结构以及流程 ①.前端控制器:只需要在web.xml文件中配置即可 作用:接受请求,处理响应结果,转发器,中央处理器 ②.处理器映射器:根据请求的url找到相应的Handl ...

  10. Xcode 7提示App Transport Security has blocked a cleartext HTTP (http://) resource load的解决办法

    Xcode 7提示App Transport Security has blocked a cleartext HTTP (http://) resource load的解决办法   今天使用Xcod ...