码迷,mamicode.com
首页 > 其他好文 > 详细

Project Euler:Problem 34 Digit factorials

时间:2017-08-07 20:39:37      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:stream   tmp   its   factor   uri   info   while   git   which   

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

标签:stream   tmp   its   factor   uri   info   while   git   which   

原文地址:http://www.cnblogs.com/wzjhoutai/p/7300798.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!