- /* Power digit sum -- Problem 16
- * 2^15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.
- * What is the sum of the digits of the number 2^1000?
- * Answer: 1366
- */
- #include <stdio.h>
- #include <time.h>
- #define MAX 1000
- int power(int, int, int[]);
- int main(void)
- {
- int base, exponent, result[MAX] = {0};
- int i, j, ans = 0, st;
- printf("Enter base and exponet: ");
- scanf("%d%d", &base, &exponent);
- st = clock();
- power(base, exponent, result);
- i = 0; while (result[i++] == 0);
- for (i--;i<MAX;i++){
- printf("%d",result[i]);
- ans += result[i];
- }
- printf("\\nAnswer: %d\\n", ans);
- printf("\\n\\nused time: %lfs\\n", (double)(clock()-st)/1000.0);
- return 0;
- }
- int power(int x, int y, int z[])
- {
- int i, j, remaind = 0, digits;
- z[MAX-1] = x;
- for (i=1; i<y; i++){
- for (j=MAX-1;j>=0;j--){
- z[j] = z[j]*x+remaind;
- remaind = z[j] / 10;
- z[j] = z[j] % 10;
- //digits = 0;
- //while (z[digits++] == 0);
- //digits--; if (j < digits) break;
- //if (digits>MAX) {printf("out of range.\\n"); exit(0);}
- }
- }
- return digits;
- }
- //该片段来自于http://www.codesnippet.cn/detail/140320149031.html
来源: http://www.codesnippet.cn/detail/140320149031.html