Chinaunix首页 | 论坛 | 博客
  • 博客访问: 238961
  • 博文数量: 47
  • 博客积分: 1229
  • 博客等级: 中尉
  • 技术积分: 568
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-20 10:06
文章分类

全部博文(47)

文章存档

2014年(1)

2013年(7)

2012年(1)

2011年(38)

分类: C/C++

2011-03-04 23:43:55

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?
--------------------
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>

  4. typedef struct _digit {
  5.     int base;
  6.     char val;
  7.     struct _digit *next;
  8. } digit;


  9. digit *create_digit(char *d)
  10. {
  11.     digit *head;
  12.     digit *node, *pnode=NULL;
  13.     int i;

  14.     for (i=strlen(d)-1; i >= 0 ; i--) {
  15.         node = malloc(sizeof(digit));
  16.         node->val = d[i];
  17.         node->next = NULL;

  18.         if (pnode) {
  19.             pnode->next = node;
  20.             pnode = node;
  21.         } else {
  22.             pnode = node;
  23.             head = node;
  24.         }
  25.     }

  26.     return head;
  27. }

  28. digit *mul2(const digit *n)
  29. {
  30.     digit *result = NULL;
  31.     const digit *pn = n;
  32.     int carry = 0;
  33.     digit *node, *pnode = NULL;

  34.     for (; pn; pn = pn->next) {

  35.         carry += (pn->val - '0') * 2;

  36.         node = malloc(sizeof(digit));
  37.         node->val = (carry%10) + '0';
  38.         node->next = NULL;

  39.         if (pnode) {
  40.             pnode->next = node;
  41.             pnode = node;
  42.         } else {    /* first node */
  43.             result = node;
  44.             pnode = node;
  45.         }

  46.         carry /= 10;
  47.     }

  48.     while (carry) {
  49.         node = malloc(sizeof(digit));
  50.         node->val = (carry%10) + '0';
  51.         node->next = NULL;

  52.         pnode->next = node;
  53.         pnode = node;

  54.         carry /= 10;
  55.     }


  56.     return result;
  57. }

  58. void free_digit(digit *p)
  59. {
  60.     digit *node=p;

  61.     while (node) {
  62.         digit *next = node->next;
  63.         free(node);
  64.         node = next;
  65.     }
  66. }

  67. /* invert output */
  68. void output_digit(digit *head)
  69. {
  70.     digit *node = head;
  71.     
  72.     if (!head)
  73.         return;

  74.     output_digit(head->next);
  75.     putchar(node->val);
  76. }

  77. #define N 1000
  78. int main(int argc, const char *argv[])
  79. {
  80.     digit *p;
  81.     digit *mul;
  82.     int i;
  83.     long long sum = 0;


  84.     mul = create_digit("2");
  85.     for (i = 1; i<N; i++) {
  86.         p = mul;
  87.         mul = mul2(mul);
  88.         free_digit(p);
  89.     }

  90.     output_digit(mul);
  91.     putchar('\n');

  92.     p = mul;
  93.     while(p) {
  94.         sum += (p->val - '0');
  95.         p = p->next;
  96.     }

  97.     printf("sum: %lld\n", sum);

  98.     free(mul);
  99.     return 0;
  100. }

  101. 效率比bc要慢。计算2^100000, bc几乎是瞬时完成的,而这个程序消耗了2m22s。
  102. 在N>10000时,感觉明显,采用预分配而不是每次malloc()?
  103. 考虑如何实现任意精度的两个数相乘?

阅读(1239) | 评论(0) | 转发(0) |
0

上一篇:euler14

下一篇:euler15

给主人留下些什么吧!~~