当前位置:网站首页>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;
}
边栏推荐
- 01仿B站项目业务架构
- Gpiof6, 7, 8 configuration
- LeetCode - 895 最大频率栈(设计- 哈希表+优先队列 哈希表 + 栈) *
- LeetCode - 933 最近的请求次数
- Serial communication based on 51 single chip microcomputer
- LeetCode - 706 设计哈希映射(设计) *
- . DLL and Differences between lib files
- Problems encountered when MySQL saves CSV files
- Positive and negative sample division and architecture understanding in image classification and target detection
- 03 FastJson 解决循环引用
猜你喜欢
Opencv Harris corner detection
Leetcode - 1670 conception de la file d'attente avant, moyenne et arrière (conception - deux files d'attente à double extrémité)
QT self drawing button with bubbles
2.Elment Ui 日期选择器 格式化问题
CV learning notes - edge extraction
My notes on the development of intelligent charging pile (III): overview of the overall design of the system software
Open Euler Kernel Technology Sharing - Issue 1 - kdump Basic Principles, use and Case Introduction
Pymssql controls SQL for Chinese queries
Blue Bridge Cup for migrant workers majoring in electronic information engineering
Crash工具基本使用及实战分享
随机推荐
Timer and counter of 51 single chip microcomputer
QT detection card reader analog keyboard input
Adaptiveavgpool1d internal implementation
4G module IMEI of charging pile design
openEuler kernel 技术分享 - 第1期 - kdump 基本原理、使用及案例介绍
pycharm 无法引入自定义包
CV learning notes convolutional neural network
Leetcode - 933 number of recent requests
QT self drawing button with bubbles
Opencv feature extraction sift
Development of intelligent charging pile (I): overview of the overall design of the system
Leetcode interview question 17.20 Continuous median (large top pile + small top pile)
2312、卖木头块 | 面试官与狂徒张三的那些事(leetcode,附思维导图 + 全部解法)
Google browser plug-in recommendation
Installation and removal of MySQL under Windows
QT is a method of batch modifying the style of a certain type of control after naming the control
Leetcode - 895 maximum frequency stack (Design - hash table + priority queue hash table + stack)*
2. Elment UI date selector formatting problem
Working mode of 80C51 Serial Port
Pycharm cannot import custom package