▼页尾

[Project Euler] Problem 30

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

1634 = 14 + 64 + 34 + 44
8208 = 84 + 24 + 04 + 84
9474 = 94 + 44 + 74 + 44

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>
usingnamespace 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;
}
posted @ 2011-06-29 10:57  xiatwhu  阅读(429)  评论(0编辑  收藏  举报
▲页首
西