当前位置:网站首页>Li Kou daily question - day 31 -202 Happy number
Li Kou daily question - day 31 -202 Happy number
2022-07-01 07:48:00 【Chongyou research Sen】
2022.6.30 Did you brush the questions today ?
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 this process The result is 1, So this number is the happy number .
If n yes Happy number Just go back to true ; No , Then return to false .
analysis :
Give you a number , You need to take it apart in this way , Then count each square , Add up all the numbers , Finally, judge whether this number is =1, by 1 Is the number of happy , Not for 1 It's not .
Ideas : Use numbers while In this way, separate and average , Finally, judge the result .
analysis :
class Solution {
public:
bool isHappy(int n) {
for (int i = 0; i < 100; i++)
{
int ans = 0;
while (n > 0)
{
ans += (n % 10) * (n % 10);
n = n / 10;
}
n = ans;
if (n == 1)
return true;
}
return false;
}
};边栏推荐
- 【批处理DOS-CMD-汇总】扩展变量-延迟变量cmd /v:on、cmd /v:off、setlocal enabledelayedexpansion、DisableDelayedExpansion
- Autosar 学习记录(1) – EcuM_Init
- iNFTnews | 从《雪崩》到百度“希壤”,元宇宙30年的16件大事
- Caesar
- weback5基础配置详解
- Subclasses call methods and properties of the parent class with the same name
- IMDB practice of emotion classification (simplernn, LSTM, Gru)
- Custom events of components ②
- Huawei modelarts training alexnet model
- Inftnews | from "avalanche" to Baidu "xirang", 16 major events of the meta universe in 30 years
猜你喜欢

The computer has a network, but all browser pages can't be opened. What's the matter?

Caesar

2022广东省安全员A证第三批(主要负责人)特种作业证考试题库模拟考试平台操作

H5 页面设置了字体的粗细样式,但是在华为手机里微信打开访问样式不生效?

她就是那个「别人家的HR」|ONES 人物

AUTOSAR learning record (1) – ECUM_ Init

Introduction to kubernetes resource objects and common commands (II)

Inftnews | from "avalanche" to Baidu "xirang", 16 major events of the meta universe in 30 years
![[recommendation system] breakthrough and imagination of deep location interactive network dpin for meituan takeout recommendation scenario](/img/10/ed857892d2e0ea72e100a4008e6d69.png)
[recommendation system] breakthrough and imagination of deep location interactive network dpin for meituan takeout recommendation scenario

Redisson uses the complete solution - redisson official documents + Notes (Part 1)
随机推荐
How to make the two financial transactions faster
Long way to go with technology
base64
Eigen matrix operation Library
[MySQL learning notes 26] view
STM32 uses esp01s to go to the cloud, mqtt FX debugging
Redisson uses the complete solution - redisson official documents + Notes (Part 1)
Wang Yingqi, founder of ones, talks to fortune (Chinese version): is there any excellent software in China?
Félicitations pour l'inscription réussie de wuxinghe
AUTOSAR learning record (1) – ECUM_ Init
Stepsister becomes stepmother, son breaks off relationship with himself, and musk, the world's richest man, why is it so miserable?
华为ModelArts训练Alexnet模型
Do securities account opening affect the security of account opening
【批处理DOS-CMD命令-汇总和小结】-Cmd窗口中常用操作符(<、<<、&<、>、>>、&>、&、&&、||、|、()、;、@)
Software testing methods and techniques - overview of basic knowledge
Missing API interface actual development series (14): ID card real name authentication verification
038 network security JS
关系数据库如何工作
力扣每日一题-第32天-1822.数组元素积的符号
赌上了绩效,赢了公司CTO,我要搭DevOps平台!