/*
* @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. 高逼格UILabel的闪烁动画效果

    高逼格UILabel的闪烁动画效果 最终效果图如下: 源码: YXLabel.h 与  YXLabel.m // // YXLabel.h // // Created by YouXianMing o ...

  2. Python学习---模版/包的概念

    1.1. 模块/包的概念 在Python中,一个.py文件就称之为一个模块(Module) 模块一共三种: python标准库 第三方模块 应用程序自定义模块 模块的使用:模块是用来组织函数的 解释器 ...

  3. 【pbrt】在c++程序中使用pbrt进行渲染

    近段时间做一个关于水面的动画.由于我用c++实现水面动画的,然而使用c++我自己的渲染系统渲染结果被同学说是可视化不叫渲染,所以我决定修改一下…… 恰好进来在学习pbrt,所以索性就蛋疼了考虑直接用p ...

  4. [原]Ubuntu 下安装Mongodb

    Mongodb是一款开源的数据库,这里不用我多说了,下面说一下Ubuntu下安装Mongodb可能遇到的问题和解决方案. 故事背景: 今天M$促销,1¥Windows Azure 4000¥-30天的 ...

  5. Shodan搜索引擎介绍

    from:http://www.exploit-db.com/wp-content/themes/exploit/docs/33859.pdf 0x00 概要 这篇文章可以作为渗透测试人员和安全工作者 ...

  6. web API之post参数传递

    最近公司开发一个新产品,前端用的vue+element,后端用的.net core ,刚开始接触这块,接口写完后在调用接口时总出现问题,尤其是post传递参数时,很多时候都获取不到参数,没办法接口都要 ...

  7. jq实现随机显示部分图片在页面上(兼容IE5)

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  8. SpringMVC(1)

    1.简要说明: Spring为展现层提供的基于MVC设计理念的优秀web框架,目前主流的框架 Spring3.0以后全面超越Struts2,成为最优秀的MVC框架 Spring MVC通过一套MVC注 ...

  9. if not

    if not x 在python中的意思是如果x为空 a = [] if not a: print 1 此代码会打印出1 a = [1,2,3] if not a: print 1 此代码不会打印出1 ...

  10. CVPR 2016 paper reading (3)

    DeepFashion: Powering Robust Clothes Recognition and Retrieval with Rich Annotations, Ziwei Liu, Pin ...