project euler problem 30

来源:互联网 发布:数据库primary key 编辑:程序博客网 时间:2024/05/04 11:22

Digit fifth powers

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.


Answer:
443839Completed on Sun, 20 Oct 2013, 07:06

题意:求各位数字的5次方之和与原数是否相同,相同则加起来,最后其和为多少?

暴力解决……

#include <iostream>#include <map>#include <deque>#include <queue>#include <stack>#include <string>#include <cstring>#include <cstdio>#include <cmath>#include <algorithm>#include <map>#include <set>using namespace std;int main(){    int i,j,n,sum,sum1=0;    for(n=2;n<=10000000;n++)    {        j=n;        sum=0;        while(j)        {            int t=j%10;            sum+=t*t*t*t*t;            j/=10;        }        if(sum==n) sum1+=sum;    }    cout<<sum1<<endl;    return 0;}

原创粉丝点击