HappyNum
/*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的更多相关文章
- C# 通俗说 委托(和事件)
1.闲聊 编码一两年, 我走过了字段, 我跑过了类, 却翻不过方法.(不能灵活使用方法吧) (写这篇博客全程听将夜中<永夜>歌曲写完的,一气呵成,安利一下) 2.叙事 我们在编码中,经常捣 ...
随机推荐
- python简单粗暴多进程之concurrent.futures
python在前面写过多线程的库threading: python3多线程趣味详解 但是今天发现一个封装得更加简单暴力的多进程库concurrent.futures: # !/usr/bin/pyth ...
- SpringMVC+Apache Shiro+JPA(hibernate)
http://my.oschina.net/moziqi/blog/305412 http://my.oschina.net/miger/blog/283526 spring4.1.0+spring ...
- ORACLE 常用数值函数
1 ABS(n)返回数值弄参数的绝对值.它接受一个数值型值作为输入参数,或者任何可以隐式地转换为数值型值的值.并且返回数值型值的绝对值. Select abs(-1) from dual ABS(-1 ...
- GitHub简单使用入门
自从google code关闭了下载服务了之后,GitHub作为了目前最好用的免费开源项目托管站点,众多开源项目都托管在github,其中不乏著名的播放器MPC-HC. 不习惯于英文的朋友,难免少不了 ...
- Android 文件的选择
Android 文件的选择 打开文件选择器 private void showFileChooser() { Intent intent = new Intent(Intent.ACTION_GET_ ...
- 微信公众平台Php版php开发(转)
http://www.1990c.com/?p=932 近在做微信公众平台开发,一口气写了二十几个功能,挺有意思的- 今天来分享一下开发经验~微信公众平台提供的接口很简单,先看看消息交互流程: 说 ...
- UI-UIImageView和Image的区别
1.UIImageView图片视图控件 继承于UIView 用于显示图片在应用程序中 2.UIImage 是将真实图片文件转化为程序中的图片,然后3.UIImageView是Image的载体,负责显示 ...
- [转]Neutron演进
在OpenStack世界中,网络组件最初叫nova-network,它混迹于计算节点nova的代码库中.nova-network可以单独部 署在一台机器上,为了高性能HA也可以和nova-comput ...
- Photoshop CS3 如何汉化
1. 下载汉化包 http://download.csdn.net/detail/yangtian1158/8740959 2. 将下载的.bat文件放到CS3的安装目录里即可 C:\Program ...
- This implementation is not part of the Windows Platform FIPS validated cryptographic algorithms
异常消息:This implementation is not part of the Windows Platform FIPS validated cryptographic algorithms ...