Project Euler Problem 16 Power digit sum
2017-03-22 19:27
393 查看
Power digit sum
Problem 16
215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.
What is the sum of the digits of the number 21000?
C++:
#include <iostream>
#include <cstring>
using namespace std;
const int MAXN = 1000;
int result[MAXN+1];
int power(int a, int n)
{
int digits, j;
memset(result, 0, sizeof(result));
result[0] = a;
digits = 1;
for(int i=2; i<=n; i++) {
int carry = 0;
for(j=0; j<digits; j++)
result[j] *= a;
for(j=0; j<=digits; j++) {
result[j] += carry;
carry = result[j] / 10;
result[j] %= 10;
}
digits = j;
}
return digits;
}
int main()
{
int n, digits;
while(cin >> n) {
digits = power(2, n);
int sum = 0;
for(int i=0; i<=digits; i++)
sum += result[i];
cout << sum << endl;
}
return 0;
}
Problem 16
215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.
What is the sum of the digits of the number 21000?
C++:
#include <iostream>
#include <cstring>
using namespace std;
const int MAXN = 1000;
int result[MAXN+1];
int power(int a, int n)
{
int digits, j;
memset(result, 0, sizeof(result));
result[0] = a;
digits = 1;
for(int i=2; i<=n; i++) {
int carry = 0;
for(j=0; j<digits; j++)
result[j] *= a;
for(j=0; j<=digits; j++) {
result[j] += carry;
carry = result[j] / 10;
result[j] %= 10;
}
digits = j;
}
return digits;
}
int main()
{
int n, digits;
while(cin >> n) {
digits = power(2, n);
int sum = 0;
for(int i=0; i<=digits; i++)
sum += result[i];
cout << sum << endl;
}
return 0;
}
相关文章推荐
- Matlab 编程 Project Euler Problem 20 Factorial digit sum
- Project Euler:Problem 16 Power digit sum
- Problem 16 Power digit sum
- projecteuler---->problem=16----Power digit sum
- Problem 16:Power digit sum
- Problem 16:Power digit sum
- Project Euler Problem 20 Factorial digit sum
- ProjectEuler-Problem 16-Power digit sum
- Problem 16 Power digit sum (高精度幂)
- Project Euler problem 16
- Project Euler Problem 6: Sum square difference
- Project Euler Problem 25 :1000-digit Fibonacci number
- http://projecteuler.net/problem=16 [Answer:1366]
- Project Euler Problem 18 Maximum path sum I
- Project Euler 16: Power digit sum.
- project euler problem 16:2^1000结果各位数求和
- Project Euler Problem 16
- Project Euler Problem 67 Maximum path sum II
- Project Euler Problem 543 Prime-Sum Numbers
- Maximum path sum I [Project Euler.Problem 18]