当前位置:网站首页>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;
}边栏推荐
- 03 FastJson 解决循环引用
- It is difficult to quantify the extent to which a single-chip computer can find a job
- Opencv notes 20 PCA
- Window maximum and minimum settings
- LeetCode - 705 设计哈希集合(设计)
- Serial port programming
- Opencv gray histogram, histogram specification
- 4G module at command communication package interface designed by charging pile
- Swing transformer details-1
- 2. Elment UI date selector formatting problem
猜你喜欢

1. Finite Markov Decision Process

Crash工具基本使用及实战分享

Dictionary tree prefix tree trie

LeetCode - 703 数据流中的第 K 大元素(设计 - 优先队列)

el-table X轴方向(横向)滚动条默认滑到右边

Leetcode - 895 maximum frequency stack (Design - hash table + priority queue hash table + stack)*

CV learning notes - reasoning and training

Mobile phones are a kind of MCU, but the hardware it uses is not 51 chip

Leetcode interview question 17.20 Continuous median (large top pile + small top pile)

openEuler kernel 技术分享 - 第1期 - kdump 基本原理、使用及案例介绍
随机推荐
[keil5 debugging] warning:enumerated type mixed with other type
Basic use and actual combat sharing of crash tool
Stm32f407 key interrupt
Screen display of charging pile design -- led driver ta6932
Blue Bridge Cup for migrant workers majoring in electronic information engineering
Emballage automatique et déballage compris? Quel est le principe?
QT is a method of batch modifying the style of a certain type of control after naming the control
自動裝箱與拆箱了解嗎?原理是什麼?
The data read by pandas is saved to the MySQL database
Window maximum and minimum settings
2020-08-23
Opencv histogram equalization
LeetCode - 703 数据流中的第 K 大元素(设计 - 优先队列)
My notes on intelligent charging pile development (II): overview of system hardware circuit design
Leetcode interview question 17.20 Continuous median (large top pile + small top pile)
Working mode of 80C51 Serial Port
On the problem of reference assignment to reference
2. Elment UI date selector formatting problem
Notes on C language learning of migrant workers majoring in electronic information engineering
Open Euler Kernel Technology Sharing - Issue 1 - kdump Basic Principles, use and Case Introduction