일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- EOI
- 매트랩
- 조지아텍
- 컴퓨터과학과
- 딥러닝
- machine learning
- LAA
- C++
- MATLAB
- omscs
- 온라인석사
- 방송통신대학교
- Deep learning
- 기본
- leetcode
- 선형대수
- 개발자
- mpnp
- 프로그래머스
- 머신러닝
- zeros
- 알고리즘
- LV1
- cpp
- 캐나다 영주권
- 방통대
- 주정부이민
- 코딩테스트
- 마니토바
- Plotting
Archives
- Today
- Total
Byte by Byte
202. Happy Number.cpp 본문
🔑 반복되는 sum이 있을 때, 무한 루프 돌게 되는 점을 착안하여 알고리즘을 구상한다.

class Solution {
public:
bool isHappy(long long n) {
int sum =0;
long long maxn =0;
int cnt =0;
while(1){
cnt++;
while(n!=0){
sum += pow(n%10,2);
n/=10;
}
n = sum;
sum =0;
if(n==maxn) return false;
if(n==1) break;
if(cnt>5 && n>maxn) maxn = max(n,maxn);
}
return true;
}
};
'개발 로그 > 알고리즘' 카테고리의 다른 글
2. Add Two Numbers.cpp (0) | 2021.11.26 |
---|---|
290. Word Pattern.cpp (0) | 2021.11.25 |
766. Toeplitz Matrix.cpp (0) | 2021.11.22 |
1700. Number of Students Unable to Eat Lunch.cpp (0) | 2021.11.21 |
349. Intersection of Two Arrays.cpp (0) | 2021.11.20 |