-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathsolve.cpp
52 lines (52 loc) · 902 Bytes
/
solve.cpp
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
47
48
49
50
51
52
#include <cstdio>
#include <string>
#include <vector>
#include <unordered_set>
using namespace std;
class Solution {
public:
bool isHappy(int n) {
return isHappy1(n);
}
private:
bool isHappy1(int n) {
unordered_set<int> s;
if (n == 0)
return false;
while (n != 1) {
s.insert(n);
n = next(n);
if (s.find(n) != s.end()) {
return false;
}
}
return true;
}
bool isHappy2(int n) {
int slow = next(n);
int fast = next(next(n));
while (slow != 1 && slow != fast) {
slow = next(slow);
fast = next(next(fast));
}
return slow == 1;
}
int next(int n) {
int ans = 0;
while (n) {
int mod = (n % 10);
ans += (mod * mod);
n /= 10;
}
return ans;
}
};
int main(int argc, char **argv)
{
Solution solution;
int n;
while (scanf("%d", &n) != EOF) {
printf("%d : %d\n", n, solution.isHappy(n));
}
return 0;
}