当前位置:网站首页>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;
}边栏推荐
- 2021-01-03
- CV learning notes - clustering
- CV learning notes - edge extraction
- Replace the files under the folder with sed
- Leetcode interview question 17.20 Continuous median (large top pile + small top pile)
- Tensorflow2.0 save model
- 使用密钥对的形式连接阿里云服务器
- Octave instructions
- Basic use and actual combat sharing of crash tool
- Swing transformer details-1
猜你喜欢

Of course, the most widely used 8-bit single chip microcomputer is also the single chip microcomputer that beginners are most easy to learn

Adaptiveavgpool1d internal implementation

Opencv Harris corner detection

openEuler kernel 技术分享 - 第1期 - kdump 基本原理、使用及案例介绍

Leetcode - 933 number of recent requests

Opencv note 21 frequency domain filtering

Timer and counter of 51 single chip microcomputer

LeetCode - 705 设计哈希集合(设计)

My notes on intelligent charging pile development (II): overview of system hardware circuit design

LeetCode - 5 最长回文子串
随机推荐
openEuler kernel 技术分享 - 第1期 - kdump 基本原理、使用及案例介绍
2020-08-23
2021-11-11 standard thread library
CV learning notes ransca & image similarity comparison hash
ADS simulation design of class AB RF power amplifier
Windows下MySQL的安装和删除
CV learning notes - deep learning
It is difficult to quantify the extent to which a single-chip computer can find a job
LeetCode - 5 最长回文子串
使用密钥对的形式连接阿里云服务器
Opencv notes 17 template matching
Dictionary tree prefix tree trie
Mobile phones are a kind of MCU, but the hardware it uses is not 51 chip
LeetCode 面试题 17.20. 连续中值(大顶堆+小顶堆)
01仿B站项目业务架构
(2)接口中新增的方法
My 4G smart charging pile gateway design and development related articles
Tensorflow built-in evaluation
[keil5 debugging] warning:enumerated type mixed with other type
Replace the files under the folder with sed