当前位置:网站首页>20220531数学:快乐数
20220531数学:快乐数
2022-07-03 09:20:00 【丿SeeYouAgain】
题目描述:编写一个算法来判断一个数 n 是不是快乐数。
快乐数定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和。然后重复这个过程直到这个数变为 1,也可能是 无限循环 但始终变不到 1。如果这个过程 结果为 1,那么这个数就是快乐数。
如果 n 是 快乐数 就返回 true ;不是,则返回 false 。
编码实现:
public static boolean isHappy(int n) {
if (n == 1) {
return true;
} else {
Set<Integer> set = new HashSet<>();
while (n != 1) {
int temp = n, sum = 0;
while (temp >= 10) {
int mod = temp % 10;
temp /= 10;
sum += mod * mod;
}
sum += temp * temp;
if (!set.add(sum)) {
return false;
}
n = sum;
}
}
return true;
}边栏推荐
- Adaptiveavgpool1d internal implementation
- [keil5 debugging] warning:enumerated type mixed with other type
- Design of charging pile mqtt transplantation based on 4G EC20 module
- Basic use and actual combat sharing of crash tool
- Cases of OpenCV image enhancement
- yocto 技術分享第四期:自定義增加軟件包支持
- RESNET code details
- CV learning notes alexnet
- LeetCode - 5 最长回文子串
- CV learning notes - edge extraction
猜你喜欢

03 fastjason solves circular references

QT is a method of batch modifying the style of a certain type of control after naming the control

Leetcode bit operation

CV learning notes ransca & image similarity comparison hash

Adaptiveavgpool1d internal implementation

SCM is now overwhelming, a wide variety, so that developers are overwhelmed

Connect Alibaba cloud servers in the form of key pairs

LeetCode - 1670 設計前中後隊列(設計 - 兩個雙端隊列)

CV learning notes alexnet

2.Elment Ui 日期选择器 格式化问题
随机推荐
Yocto technology sharing phase IV: customize and add software package support
SCM is now overwhelming, a wide variety, so that developers are overwhelmed
03 fastjason solves circular references
Emballage automatique et déballage compris? Quel est le principe?
Application of 51 single chip microcomputer timer
YOLO_ V1 summary
1. Finite Markov Decision Process
Opencv interview guide
openEuler kernel 技術分享 - 第1期 - kdump 基本原理、使用及案例介紹
2020-08-23
Mobile phones are a kind of MCU, but the hardware it uses is not 51 chip
[combinatorics] combinatorial existence theorem (three combinatorial existence theorems | finite poset decomposition theorem | Ramsey theorem | existence theorem of different representative systems |
yocto 技术分享第四期:自定义增加软件包支持
Leetcode - 895 maximum frequency stack (Design - hash table + priority queue hash table + stack)*
LeetCode - 715. Range 模块(TreeSet) *****
RESNET code details
yocto 技術分享第四期:自定義增加軟件包支持
Markdown latex full quantifier and existential quantifier (for all, existential)
01仿B站项目业务架构
Basic use and actual combat sharing of crash tool