`

(Problem 16)Power digit sum

阅读更多

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 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics