趣味水仙花数

 1 public class Yihuo {
 2     public static void sun(int n) {
 3         long start,end,sum,num,i,temp;
 4         start=(long)Math.pow(10, n-1);
 5         end=(long)Math.pow(10,n)-1;
 6         int j;
 7         for(i=start;i<=end;i++) {
 8             temp=0;
 9             num=i;
10             sum=0;
11             for(j=0;j<n;j++) {
12                 temp=num%10;
13                 sum=sum+(long)Math.pow(temp, n);
14                 num=(num-temp)/10;
15             }
16             if(sum==i) {
17                 System.out.printf("%d\n",i);
18             }
19         }
20     }
21     
22     public static void main(String []args) {
23         int n;
24         n=3;
25         System.out.printf("列举%d位的水仙花数: \n",n);
26         sun(n);
27         n=4;
28         System.out.printf("列举%d位的水仙花数::\n",n);
29         sun(n);
30         
31 }}

猜你喜欢

转载自www.cnblogs.com/mianyang0902/p/10702368.html