当前位置:网站首页>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;
}
};边栏推荐
- [recommendation system] breakthrough and imagination of deep location interactive network dpin for meituan takeout recommendation scenario
- Sorting out tcp/udp communication problems
- 力扣每日一题-第31天-1502.判断能否形成等差数列
- Vscode automatically formats code according to eslint specification
- [programming training] delete public characters (hash mapping) + team competition (greedy)
- [skill] create Bat quick open web page
- How to make the two financial transactions faster
- Thesis learning -- Analysis and Research on similarity query of hydrological time series
- 力扣每日一题-第31天-1790.仅执行一次字符串交换能否使两个字符串相等
- Cadence OrCAD Capture “网络名”相同,但是未连接或连接错误的解放方案之nodename的用法
猜你喜欢

Gui Gui programming (XV) - use scale to control font size changes

Apple账号密码自动填充

【无标题】
![[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](/img/5e/068268857b8c7c2065ba552fd23dbb.png)
[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

AUTOSAR learning record (1) – ECUM_ Init

QT -- 1. QT connection database

2022 test questions and mock examinations for main principals of hazardous chemicals business units

Eigen matrix operation Library

C # read and write customized config file

浏览器本地存储
随机推荐
iNFTnews | 从《雪崩》到百度“希壤”,元宇宙30年的16件大事
Stepsister becomes stepmother, son breaks off relationship with himself, and musk, the world's richest man, why is it so miserable?
[chapter 72 of the flutter problem series] a solution to the problem that pictures taken in the flutter using the camera plug-in are stretched
力扣——求一组字符中的第一个回文字符
华泰证券开户是安全可靠的么?怎么开华泰证券账户
【推荐系统】美团外卖推荐场景的深度位置交互网络DPIN的突破与畅想
ctfshow-web352,353(SSRF)
Redisson uses the full solution - redisson official documents + comments (Part 2)
Software testing methods and techniques - overview of basic knowledge
Browser local storage
Redisson uses the full solution - redisson official document + comments (Part 2)
2022电工(中级)复训题库及答案
【R语言】两个/N个数据合并merge函数
C# Newtonsoft. Use of job in JSON
redisson使用全解——redisson官方文档+注释(下篇)
Introduction to kubernetes resource objects and common commands (II)
[MySQL learning notes 28] storage function
How to create an exclusive vs Code theme
【微服务|openfeign】Feign的日志记录
The H5 page has set the font thickness style, but the wechat access style in Huawei mobile phone doesn't take effect?