202 Happy Number

来源:互联网 发布:猎手软件下载 编辑:程序博客网 时间:2024/04/28 12:33
public static boolean isHappy(int n) {        HashSet<Integer> al = new HashSet<Integer>();        while (n != 1) {            n = func(n);            if (!al.add(n))                return false;        }        return true;    }    public static int func(int n) {        int temp = 0;        while (n > 0) {            temp += ((n % 10) * (n % 10));//注意括号的使用,不要写成(n % 10 * n % 10)            n = n / 10;        }        return temp;    }
0 0
原创粉丝点击