2010年8月12日 星期四

高中 a040: 阿姆斯壯數



想看題目請點我

#include <iostream>
#include <math.h>
using namespace std;

main() {
     int n , m ;
     while(cin >> n >> m)
     {
         if((n0) && (m<=1000000))
         { 
             int flag = 0 ;
             for(int i = n ; i<=m ; i++)
             {
                 int number[7] = { 0,0,0,0,0,0,0 } , value = i , length = 0 ;
                 //1634 = 1^4 + 6^4 + 3^4+ 4^4 取出1634 
                 for(int j = 0 ; value !=0 ; j++)
                 {
                     number[j] = value % 10 ;
                     value /= 10 ;
                     length++ ;
                 }
                 // 次方判斷
                 for(int j = 1 ; j <= length ; j++ )
                 {
                      value = pow(number[6],j)+pow(number[5],j)+pow(number[4],j)+pow(number[3],j)+pow(number[2],j)+pow(number[1],j)+pow(number[0],j) ;
                      if( value == i )
                      {
                          cout << i << " " ;
                          flag = 1 ;
                      }
                 }
             }
             if(flag == 0) cout << "none" ;
             cout << endl ;
         }
     }
}