2016年1月13日 星期三

a040: 阿姆斯壯數

內容 :
所謂 Armstrong number 指的是一個 n 位數的整數,它的所有位數的 n 次方和恰好等於自己。
如;1634 = 14 + 6+ 34+ 44
請依題目需求在一定範圍內找出該範圍內的所有 armstrong numbers.
輸入說明 : 
輸入包含兩個數字n, m(n<m, n>0, m<=1000000),代表所有尋找 armstrong number 的範圍
輸出說明 : 
將所有範圍內的 armstrong number 依序由小到大輸出,如果沒有找到請輸出 none.
範例輸入 : help
100 999
10 99
範例輸出:
153 370 371 407
none
標籤:

import java.util.*;
public class a040 {                            
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n,m,a,i,j,c;
int nm[]=new int[10];
double total=0;
        boolean flag=false;
        while(sc.hasNext()){
        n=sc.nextInt(); m=sc.nextInt();
        flag=false;
   for(i=n;i<=m;i++){
 a=i; c=0; total=0;
 while(a>0){
nm[c++]=a%10;
a=a/10;
 }
 for(j=0;j<c;j++){
total+=Math.pow(nm[j],c);
 }
 if(total==i){
System.out.print((int)total+" ");
flag=true;
 }
}
if(flag==false)
System.out.print("none");
System.out.println();
        }
    }
}

沒有留言:

張貼留言