Project Euler Problem 16 Power digit sum

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;
}





posted on 2017-03-22 19:28  海岛Blog  阅读(135)  评论(0编辑  收藏  举报

导航