当前位置:网站首页>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;
}
};边栏推荐
- 2022广东省安全员A证第三批(主要负责人)特种作业证考试题库模拟考试平台操作
- Alibaba OSS postman invalid according to policy: policy condition failed: ["starts with", "key", "test/"]
- 浏览器本地存储
- atguigu----脚手架--02-使用脚手架(2)
- Subclasses call methods and properties of the parent class with the same name
- 【mysql学习笔记28】存储函数
- [programming compulsory training 3] find the longest consecutive number string in the string + the number that appears more than half of the times in the array
- IMDB practice of emotion classification (simplernn, LSTM, Gru)
- Introduction to kubernetes resource objects and common commands (II)
- 力扣——求一组字符中的第一个回文字符
猜你喜欢

Array: question brushing record

赌上了绩效,赢了公司CTO,我要搭DevOps平台!

C# Newtonsoft.Json中JObject的使用

Why some people earn nearly 10billion a year, while others earn 3000 a month: the details you ignore actually make the most money

Are there any practical skills for operation and maintenance management

redisson使用全解——redisson官方文檔+注釋(上篇)

Wang Yingqi, founder of ones, talks to fortune (Chinese version): is there any excellent software in China?

华为ModelArts训练Alexnet模型

The database is locked. Is there a solution

浏览器本地存储
随机推荐
2022 electrician (intermediate) recurrent training question bank and answers
weback5基础配置详解
Illusory and simple screen raindrop post-processing effect
base64
redisson使用全解——redisson官方文档+注释(下篇)
ctfshow-web355,356(SSRF)
力扣每日一题-第32天-1822.数组元素积的符号
Todolist classic case ①
[软件] phantomjs屏幕截图
【mysql学习笔记26】视图
【编程强训3】字符串中找出连续最长的数字串+数组中出现次数超过一半的数字
Cyclic neural network
The triode is a great invention
Cadence OrCAD Capture “网络名”相同,但是未连接或连接错误的解放方案之nodename的用法
The database is locked. Is there a solution
JAX的深度学习和科学计算
[skill] create Bat quick open web page
2022茶艺师(初级)操作证考试题库及模拟考试
她就是那个「别人家的HR」|ONES 人物
[microservice openfeign] feign's log record