project euler 34 Digit factorials
2018-01-16 12:09
363 查看
题目:
https://projecteuler.net/problem=34题意:
Digit factorialsProblem 34
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.
求所有满足以下条件的数字的和:一个数等于其数位上数字阶乘的和
思路:
可以简单分析一下确定满足条件的数字的上界,当数字为7位时,比如最大的9999999的数位上数字阶乘的和只有2540160,这意味着7位数字中有解的话,也会小于2540160,大于7位的数字则不可能有解,所有只需要判断小于2540160的数字即可,然后累加满足条件的数字代码:
#include <bits/stdc++.h> using namespace std; const int N = 10 + 10; int fact ; void fact_table(int n) { fact[0] = 1; for(int i = 1; i <= n; ++i) fact[i] = fact[i-1] * i; } int main() { fact_table(9); //cout << fact[9] << endl; //cout << 7*fact[9] << endl; int n = 3000000; int ans = 0; for(int i = 10; i <= n; ++i) { int temp = 0, val = i; while(val) { temp += fact[val%10]; val /= 10; } if(temp == i) ans += i; } printf("%d\n", ans); return 0; }
相关文章推荐
- Project Euler:Problem 34 Digit factorials
- (Problem 34)Digit factorials
- Problem 34 Digit factorials (暴力)
- (Problem 34)Digit factorials
- (Problem 34)Digit factorials
- [Euler]Problem 34 - Digit factorials
- project euler 16 Power digit sum
- 欧拉工程第34题:Digit factorials
- project euler 33 Digit cancelling fractions
- Project Euler:Problem 25 1000-digit Fibonacci number
- Project Euler 34
- Project Euler:Problem 63 Powerful digit counts
- No_33:Digit factorials
- Project Euler:Problem 16 Power digit sum
- Project Euler:Problem 51 Prime digit replacements
- Project Euler:Problem 56 Powerful digit sum
- Project Euler:Problem 74 Digit factorial chains
- Project Euler 92:Square digit chains 平方数字链
- Project Euler 题解 #20 Factorial digit sum
- project euler-34