当前位置:网站首页>202. Happy number
202. Happy number
2022-04-23 10:15:00 【Popuessing's Jersey】
subject :
Write an algorithm to judge a number n Is it a happy number .
「 Happy number 」 Defined as : For a positive integer , Replace the number with the sum of the squares of the numbers in each of its positions , Then repeat the process until the number becomes 1, It could be Infinite loop But it doesn't change 1. If It can be 1, So this number is the happy number .
If n If it's happy, count it back True ; No , Then return to False .
Ideas :
Each calculated sum recorded , If there are repeated sum You can judge that you can't get the number of happiness , If there is no repetition sum value , Then it means that this calculation will continue until we find sum=1;
public class Kuaileshu {
// Take the sum of the squares of the singular numbers of each digit
public int getSum(int n){
int sum = 0;
while(n>0){
sum +=(n%10)*(n%10);
n/=10;
}
return sum;
}
public boolean isHappy(int n){
Set<Integer> record = new HashSet<>();
// If the sum of squares is not equal to 1 And there is no sum of squares in the set
// Put the sum of this square into the set
while( n!= 1 && !record.contains(n)){
record.add(n);
n = getSum(n);
}
return n==1;
}
public static void main(String[] args) {
Kuaileshu kuaileshu = new Kuaileshu();
boolean res = kuaileshu.isHappy(19);
System.out.println(res);
}
}
Output results :
True
版权声明
本文为[Popuessing's Jersey]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231011140776.html
边栏推荐
- 第120章 SQL函数 ROUND
- 2022 mobile crane driver test question bank simulation test platform operation
- DBA common SQL statements (1) - overview information
- Pyqt5 and communication
- ansible 云计算 自动化
- Question bank and answers of Shanghai safety officer C certificate examination in 2022
- Shell script interaction free
- MapReduce核心和基础Demo
- 中职网络安全2022国赛之CVE-2019-0708漏洞利用
- Jerry's users how to handle events in the simplest way [chapter]
猜你喜欢
2022 mobile crane driver test question bank simulation test platform operation
ARM调试(1):两种在keil中实现printf重定向到串口的方法
Yarn core parameter configuration
Configuration of LNMP
Failureforwardurl and failureurl
Question bank and answers of Shanghai safety officer C certificate examination in 2022
基于PyQt5实现弹出任务进度条功能示例
Detailed explanation of MapReduce calculation process
【无标题】
IDEA——》每次启动都会Indexing或 scanning files to index
随机推荐
《Redis设计与实现》
Examination questions and answers of the third batch (main person in charge) of Guangdong safety officer a certificate in 2022
454、四数之和(哈希表)
Read LSTM (long short term memory)
Formattime timestamp format conversion
Realize data value through streaming data integration (1)
C语言——自定义类型
Leetcode22:括号生成
242、有效字母异位词(哈希表)
定义链表(链表)
Function realization of printing page
Yarn resource scheduler
Jerry sometimes finds that the memory has been tampered with, but there is no exception. How should he find it? [chapter]
Juc并发编程06——深入剖析队列同步器AQS源码
[untitled]
通过流式数据集成实现数据价值(5)- 流处理
MapReduce核心和基础Demo
MapReduce core and foundation demo
net start mysql MySQL 服务正在启动 . MySQL 服务无法启动。 服务没有报告任何错误。
深度选择器