/*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

12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1*/
import java.util.*; public class HappyNum { public static void main(String[] args) {
// TODO Auto-generated method stub System.out.println(isHappy(19)); } /**
* @param n
*/ public static int pingfanghe(int n) {
int sum = 0;
while (n != 0) {
sum += (n % 10) * (n % 10);
n = n / 10;
}
return sum;
} public static boolean isHappy(int n) {
/*
* int sum=0; char[] ch=String.valueOf(n).toCharArray(); for(int
* i=0;i<ch.length;i++) sum+=(ch[i])*ch[i]; //这样不行,返回的是asc码
* System.out.println(sum); if(n==1) return true; else return
* isHappy(n);
*/
/*
* if(n==1) return true; Set<Integer> al=new HashSet<Integer>(); int
* sum=0; while(n!=0) { al.add(n%10); n=n/10; } System.out.println(al);
* for(int i=0;i<al.size();i++) sum+=al.*al.get(i); n=sum;
* System.out.println(n);
*
*
*
* // return isHappy(n);
*
* return isHappy(n);
*/ Set<Integer> hs = new HashSet<Integer>();
while (true) {
if (hs.contains(n))
return false;
if (n == 1)
return true;
hs.add(n);
n = pingfanghe(n); } } }

注意:1.循环的判断

2.字符数组转化是不行的,输出的是asc码。

3.集合框架的使用

HappyNum的更多相关文章

  1. C# 通俗说 委托(和事件)

    1.闲聊 编码一两年, 我走过了字段, 我跑过了类, 却翻不过方法.(不能灵活使用方法吧) (写这篇博客全程听将夜中<永夜>歌曲写完的,一气呵成,安利一下) 2.叙事 我们在编码中,经常捣 ...

随机推荐

  1. python compile

    compile(source, filename, mode[, flags[, dont_inherit]]) 参数source:字符串或者AST(Abstract Syntax Trees)对象. ...

  2. MySQL中Group By,distinct使用注意事项

    mysql> select * from test; +----+-------+------+-------+ | id | name | age | class | +----+------ ...

  3. 自动扫描FTP文件工具类 ScanFtp.java

    package com.util; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import ja ...

  4. Velocity语法--转载

    Velocity是一个基于java的模板引擎(template engine),它允许任何人仅仅简单的使用模板语言(template language)来引用由java代码定义的对象.作为一个比较完善 ...

  5. 用php自带的filter函数验证、过滤数据 -转载

    PHP过滤器包含两种类型 Validation:用来验证验证项是否合法 Sanitization:用来格式化被验证的项目,因此它可能会修改验证项的值,将不合法的字符删除等. input_filters ...

  6. 【python】浅谈for...else...语句

    for循环可以和 else 子句同时使用(for...else).当迭代完for循环完整个列表失败后,会执行else语句.但循环中被 break 终止的情况下不会执行.如下例子所示: for i in ...

  7. [转]开源那些事儿(四)-如何使用CodePlex进行项目管理

    本文版权信息作者:Jake Lin(Jake's Blog on 博客园) 出处:http://www.cnblogs.com/procoder/archive/2010/02/10/About-Op ...

  8. SQLite介绍、学习笔记、性能测试

    SQLite介绍.学习笔记.性能测试 哪些人,哪些公司或软件在用SQLite: Nokia's Symbian,Mozilla,Abobe,Google,阿里旺旺,飞信,Chrome,FireFox可 ...

  9. Tortoise SVN Clean up失败的解决方法

    step1: 到 sqlite官网 (http://www.sqlite.org/download.html) 下载 sqlite3.exe (找到 Precompiled Binaries for ...

  10. TSP(旅行者问题)——动态规划详解(转)

    1.问题定义 TSP问题(旅行商问题)是指旅行家要旅行n个城市,要求各个城市经历且仅经历一次然后回到出发城市,并要求所走的路程最短. 假设现在有四个城市,0,1,2,3,他们之间的代价如图一,可以存成 ...