-
Notifications
You must be signed in to change notification settings - Fork 0
/
p202.rs
37 lines (32 loc) · 894 Bytes
/
p202.rs
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
#[test]
fn test() {
use method1::is_happy;
assert_eq!(is_happy(19), true);
assert_eq!(is_happy(2), false);
}
// 数是正整数
mod method1 {
pub fn is_happy(n: i32) -> bool {
use std::collections::HashSet;
// 计算下一个数
fn get_next_num(mut n: i32) -> i32 {
let mut sum: i32 = 0;
while n != 0 {
let num: i32 = n % 10;
n = n / 10;
sum += num.pow(2);
}
sum
}
let mut n: i32 = n;
let mut set: HashSet<i32> = HashSet::new();
// 要点是,数字可能会陷入循环
// 如果 set 中已经有这个数了,代表陷入循环了
// 除此之外,都会得到 1
while n != 1 && !set.contains(&n) {
set.insert(n);
n = get_next_num(n);
}
n == 1
}
}