guangzhou



shanghai

Recent posts:
Blog index
About
RSS

Project Euler 16–Power digit sum

August 11, 2018     欧拉计划   671   

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?

—————————————————————-

题目大意:

215 = 32768 并且其各位之和为 is 3 + 2 + 7 + 6 + 8 = 26.

21000 的各位数之和是多少?

#include <stdio.h> 
#include <stdbool.h>

void solve(void)
{
    int a[100000] = {0};
    int n, sum, i, j;
    n = sum = 0;
    a[0] = 1;
    for(i = 0; i < 1000; i++) {  //以1000进制的方法存储
        for(j = 0; j <= n; j++) {
            a[j] *= 2;
        }
        for(j = 0; j <= n; j++) {
            if(a[j] >= 10000) {
                a[j] %= 10000;
                a[j+1]++;
                n++;
            }
        }
    }
    for(i = 0; i <= n; i++) {
        sum += a[i] / 10000;
        a[i] %= 10000;
        sum += a[i] / 1000;
        a[i] %= 1000;
        sum += a[i] / 100;
        a[i] %= 100;
        sum += a[i] / 10;
        a[i] %= 10;
        sum += a[i];

    }
    printf("%d\n",sum);
}

int main(void)
{
    solve();
    return 0;
}

 Answer:1366

Completed on Sun, 17 Nov 2013, 15:23

 

如果文章对您有帮助,欢迎点击下方按钮打赏作者

Comments

No comments yet.
To verify that you are human, please fill in "七"(required)