水仙花数
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; } }
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; } }