[Project Euler] Problem 30
2011-06-29 10:57
295 查看
Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:
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;
}
1634 = 14 + 64 + 34 + 44
8208 = 84 + 24 + 04 + 84
9474 = 94 + 44 + 74 + 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;
}
相关文章推荐
- Project Euler:Problem 30 Digit fifth powers
- Project Euler:Problem 1
- java 求无限循环小数的循环体/循环部分 project euler problem26
- [Project Euler] Problem 4
- [Project Euler] Problem 27
- Project Euler - Problem 13
- Project Euler:Problem 18 Maximum path sum I
- Project Euler:Problem 38 Pandigital multiples
- Project Euler:Problem 51 Prime digit replacements
- Project euler problem 21 - 30
- 【Project Euler】【Problem 6】Sum square difference
- Project Euler Problem 30
- Project Euler:Problem 42 Coded triangle numbers
- Project Euler:Problem 2
- [Project Euler]加入欧拉 Problem 15
- [Project Euler] Problem 28
- [Project Euler] Problem 18 & Problem 67
- [Project Euler] Problem 19
- [Project Euler]Problem 1:Multiples of 3 and 5
- Project Euler__problem 1