当前位置:网站首页>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;
}
};边栏推荐
- Discussion on several research hotspots of cvpr2022
- 继妹变继母,儿子与自己断绝关系,世界首富马斯克,为何这么惨?
- I bet on performance and won the CTO of the company. I want to build Devops platform!
- [skill] create Bat quick open web page
- redisson看门狗机制,redisson看门狗性能问题,redisson源码解析
- 【mysql学习笔记28】存储函数
- 2022 Guangdong Provincial Safety Officer a certificate third batch (main person in charge) special operation certificate examination question bank simulated examination platform operation
- 赌上了绩效,赢了公司CTO,我要搭DevOps平台!
- 【编程强训3】字符串中找出连续最长的数字串+数组中出现次数超过一半的数字
- Kickback -- find the first palindrome character in a group of characters
猜你喜欢

如何使用layui将数据库中的数据以表格的形式展现出来

kubernetes资源对象介绍及常用命令(二)

【目标检测】目标检测界的扛把子YOLOv5(原理详解+修炼指南)

软件测试方法和技术 - 基础知识概括

I bet on performance and won the CTO of the company. I want to build Devops platform!

Apple账号密码自动填充

The database is locked. Is there a solution

Minecraft 1.16.5模组开发(五十一) 方块实体 (Tile Entity)

Thesis learning -- Analysis and Research on similarity query of hydrological time series

TodoList经典案例①
随机推荐
What information does the supplier need to know about Audi EDI project?
[untitled]
[programming training 2] sorting subsequence + inverted string
Minecraft 1.16.5模组开发(五十一) 方块实体 (Tile Entity)
华泰证券开户是安全可靠的么?怎么开华泰证券账户
TCP/UDP 通信问题整理
2022 mobile crane driver test exercises and online simulation test
base64
Conscience Amway universal wheel SolidWorks model material website
【技能】创建.bat快速打开网页
Browser local storage
[MySQL learning notes27] stored procedure
力扣每日一题-第31天-1502.判断能否形成等差数列
力扣每日一题-第32天-1822.数组元素积的符号
Minecraft 1.16.5 module development (51) tile entity
Félicitations pour l'inscription réussie de wuxinghe
Ctfhub port scan (SSRF)
[R language] two /n data merge functions
[软件] phantomjs屏幕截图
力扣每日一题-第31天-202.快乐数