[Project Euler] Problem 30

Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:

1634 = 1 4 + 6 4 + 3 4 + 4 4
8208 = 8 4 + 2 4 + 0 4 + 8 4
9474 = 9 4 + 4 4 + 7 4 + 4 4

As 1 = 14 is not a sum it is not included.

The sum of these numbers is 1634 + 8208 + 9474 = 19316.

Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.

这道题没有太多技巧可言,直接穷举法

由于9**5*7 < 1000000;所以满足条件的数不可能是7位或以上的数

我们从2穷举到 9**5*6;

#include < iostream >
#include
< cmath >
using namespace std;

bool isGood( int );

int main(){
int sum = 0 ;
for ( int i = 2 ; i < pow( 9 , 5 ) * 6 ; i ++ ){
if (isGood(i)){
sum
+= i;
}
}
cout
<< sum << endl;

}

bool isGood( int num){
int tmp = 0 ;
int backup = num;
while (backup > 0 ){
tmp
+= pow(backup % 10 , 5 );
backup
/= 10 ;
}
return tmp == num;
}

你可能感兴趣的:(project)