Project Euler 16 Power digit sum( 大数乘法 )


题意:
215 = 32768,而32768的各位数字之和是 3 + 2 + 7 + 6 + 8 = 26。

21000的各位数字之和是多少?

思路:大数乘法,计算 210 × 100 可加速计算,每次超过1000进位


/*************************************************************************
> File Name: euler016.c
> Author: WArobot
> Blog: http://www.cnblogs.com/WArobot/
> Created Time: 2017年06月27日 星期二 20时41分24秒
************************************************************************/

include <stdio.h>

include <inttypes.h>

define D_VALUE 1000

int32_t main() {
int32_t ans[1001] = {0};
ans[0] = ans[1] = 1; // ans[0] 记录位数
for (int32_t i = 0 ; i < 100 ; i++) {
for (int32_t j = 1 ; j <= ans[0] ; j++) {
ans[j] *= 1024;
}
for (int32_t j = 1 ; j <= ans[0] ; j++) {
if (ans[j] >= D_VALUE) {
ans[j + 1] += ans[j] / D_VALUE;
ans[j] %= D_VALUE;
if (j == ans[0]) ans[0]++;
}
}
}
int32_t sum = 0;
for (int32_t i = 1 ; i <= ans[0] ; i++) {
while (ans[i]) {
sum += ans[i] % 10;
ans[i] /= 10;
}
}
printf("%d\n",sum);
return 0;
}

posted @ 2017-06-27 20:52  ojnQ  阅读(276)  评论(0)    收藏  举报