您的位置:首页 > 其它

水仙花数

2014-02-24 11:19 183 查看
public class NumberOfDaffodils {   

public static void main(String[] args)

{   

List<Long> numbers = new ArrayList<Long>();   

for(long i = 100; i < Long.MAX_VALUE ; i++)

{   if(match(i))

{   numbers.add(i);   }   

}   

System.out.println(numbers);  

 }   

private static boolean match(long in)

{   //统计位数   int count = (in+"").length();  

 //记录每位数字   long temp = 0;  

 //辗转取余的保存数   long num = in;  

 //求和数   long sum = 0;  

 for(int i = count ; i > 0 ; i--){  

 temp = num/(long)Math.pow(10,i-1);  

 num = num%(long)Math.pow(10,i-1);

  sum+=Math.pow(temp,count);   }  

 return in == sum;   }   }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: