-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisHappyNumber.h
46 lines (43 loc) · 905 Bytes
/
isHappyNumber.h
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//
// Created by so_go on 2019/6/23.
//
#ifndef SRC_ISHAPPYNUMBER_H
#define SRC_ISHAPPYNUMBER_H
#include<cmath>
#include<unordered_set>
using namespace std;
class IsHappy {
public:
bool isHappy(int n) {
unordered_set<int> encounter;
while( n != 1){
cout << n << endl;
if(encounter.find(n) != encounter.end()){
// if find
break;
}
else{
// not find
encounter.insert(n);
}
n = transform(n);
}
if(n == 1){
return true;
}
else{
return false;
}
}
int transform(int n){
int res = 0;
int tmp;
while( n > 0){
tmp = n % 10;
res += tmp * tmp;
n = n / 10;
}
return res;
}
};
#endif //SRC_ISHAPPYNUMBER_H