工学1号馆

home

« | 返回首页 | »

(Problem 16)Power digit sum

By Wu Yudong on January 15, 2017

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 的各位数之和是多少?

// (Problem 16)Power digit sum
// Completed on Sun, 17 Nov 2013, 15:23
// Language: C
//
// 版权所有(C)wuyudong
// 博客地址:http://www.wuyudong.com

#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:1336
Completed on Sun, 17 Nov 2013, 23:23

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

Comments

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