首页 > 代码库 > Project Euler:Problem 34 Digit factorials
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。