zoukankan      html  css  js  c++  java
  • Happy Number

    package cn.edu.xidian.sselab.hashtable;

    import java.util.HashSet;
    import java.util.Set;

    /**
     *
     * @author zhiyong wang
     * title: Happy Number
     * content:
     * Write an algorithm to determine if a number is "happy".
     * A happy number is a number defined by the following process:
     *             Starting with any positive integer,
     *             replace the number by the sum of the squares of its digits,
     *             and repeat the process until the number equals 1 (where it will stay),
     *             or it loops endlessly in a cycle which does not include 1.
     *             Those numbers for which this process ends in 1 are happy numbers.
     *
     *    Example: 19 is a happy number
     *
     *        1^2 + 9^2 = 82
     *        8^2 + 2^2 = 68
     *        6^2 + 8^2 = 100
     *        1^2 + 0^2 + 0^2 = 1
     *
     *
     */
    public class HappyNumber {

        //自己改了三次才改正确的方法,出现了两个错误:(1)忘记如果不能于1,会一直循环的情况给排除掉,这里用一个Set保存出现的值来排除这种情况
        //(2)求平方的时候没有用Math.pow(a,b)这种方法
        public boolean isHappy(int n){
            if(n < 1)
                return false;
            Set set = new HashSet();
            while(n > 1){
                if(!set.add(n))
                    return false;
                n = calculate(n);
            }
            if(n == 1)
                return true;
            else
                return false;
        }
        
        private int calculate(int n){
            int result = 0;
            while(n > 0){
                result += Math.pow(n%10, 2);
                n = n/10;
            }
            return result;
        }

    }

  • 相关阅读:
    Android
    Android
    Android
    Android
    Android
    【工作中学习】CreateProcessAsUser失败,错误码:1314
    【Angular JS】网站使用社会化评论插件,以及过程中碰到的坑
    【Angular JS】正确调用JQuery与Angular JS脚本
    【工作】Proxy Server的优化
    AWS ELB Sticky Session有问题?别忘了AWSELB cookie
  • 原文地址:https://www.cnblogs.com/wzyxidian/p/5158519.html
Copyright © 2011-2022 走看看