当前位置:网站首页>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-11-11 standard thread library
- Of course, the most widely used 8-bit single chip microcomputer is also the single chip microcomputer that beginners are most easy to learn
- Connect Alibaba cloud servers in the form of key pairs
- openEuler kernel 技術分享 - 第1期 - kdump 基本原理、使用及案例介紹
- [combinatorics] combinatorial existence theorem (three combinatorial existence theorems | finite poset decomposition theorem | Ramsey theorem | existence theorem of different representative systems |
- Leetcode - 933 number of recent requests
- Basic knowledge of communication interface
- Leetcode bit operation
- yocto 技术分享第四期:自定义增加软件包支持
- Basic use and actual combat sharing of crash tool
猜你喜欢

Open Euler Kernel Technology Sharing - Issue 1 - kdump Basic Principles, use and Case Introduction

Leetcode 300 最长上升子序列

LeetCode - 706 设计哈希映射(设计) *

Yocto technology sharing phase IV: customize and add software package support
![[untitled] proteus simulation of traffic lights based on 89C51 Single Chip Microcomputer](/img/90/4de927e797ec9c2bb70e507392bed0.jpg)
[untitled] proteus simulation of traffic lights based on 89C51 Single Chip Microcomputer

I think all friends should know that the basic law of learning is: from easy to difficult

CV learning notes - reasoning and training

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

Openeuler kernel technology sharing - Issue 1 - kdump basic principle, use and case introduction

LeetCode - 508. Sum of subtree elements with the most occurrences (traversal of binary tree)
随机推荐
The data read by pandas is saved to the MySQL database
Opencv image rotation
Interruption system of 51 single chip microcomputer
Installation and removal of MySQL under Windows
03 fastjason solves circular references
CV learning notes - Stereo Vision (point cloud model, spin image, 3D reconstruction)
Emballage automatique et déballage compris? Quel est le principe?
LeetCode - 706 设计哈希映射(设计) *
STM32 general timer output PWM control steering gear
Circular queue related design and implementation reference 1
Tensorflow2.0 save model
01 business structure of imitation station B project
Serial port programming
4G module at command communication package interface designed by charging pile
JS foundation - prototype prototype chain and macro task / micro task / event mechanism
(1) 什么是Lambda表达式
4G module initialization of charge point design
[untitled] proteus simulation of traffic lights based on 89C51 Single Chip Microcomputer
Gpiof6, 7, 8 configuration
51 MCU tmod and timer configuration