Project Euler:Problem 34 Digit factorials

Posted wzjhoutai

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Project Euler:Problem 34 Digit factorials相关的知识,希望对你有一定的参考价值。

145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145.

Find the sum of all numbers which are equal to the sum of the factorial of their digits.

Note: as 1! = 1 and 2! = 2 are not sums they are not included.



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

vector<int> int_vet(int a)
{
	vector<int> res;
	while (a)
	{
		int tmp = a % 10;
		a /= 10;
		res.push_back(tmp);
	}
	return res;
}

int a[10] = { 0 };
void p()
{
	a[0] = 1;
	a[1] = 1;
	for (int i = 2; i <= 9; i++)
		a[i] = a[i - 1] * i;
}

int main()
{
	p();
	int res = 0;
	for (int i = 3; i < 10000000; i++)
	{
		int count = 0;
		vector<int> num = int_vet(i);
		for (int j = 0; j < num.size(); j++)
			count += a[num[j]];
		if (count == i)
			res += count;
	}
	cout << res << endl;

	system("pause");
	return 0;
}


以上是关于Project Euler:Problem 34 Digit factorials的主要内容,如果未能解决你的问题,请参考以下文章

Project Euler:Problem 89 Roman numerals

Problem 43 // Project Euler

Project Euler :Problem 54 Poker hands

Project Euler:Problem 77 Prime summations

Project Euler:Problem 76 Counting summations

Project Euler:Problem 41 Pandigital prime