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

全部博文(47)

文章存档

2014年(1)

2013年(7)

2012年(1)

2011年(38)

分类: C/C++

2011-03-14 00:08:26

n! means n × (n − 1) × ... × 3 × 2 × 1

Find the sum of the digits in the number 100!
--------------------
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>

  4. typedef struct _digit_t {
  5.     char v;
  6.     int base;
  7.     struct _digit_t *next;
  8. } digit_t;

  9. /* invert input */
  10. digit_t *create_digit(char *d)
  11. {
  12.     digit_t *head;
  13.     digit_t *node, *pnode=NULL;
  14.     int i;

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

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

  27.     return head;
  28. }

  29. digit_t * mul(digit_t *mul1, digit_t *mul2)
  30. {
  31.     digit_t *result, *pmul1, *pmul2;
  32.     digit_t *node = NULL, *pnode = NULL;
  33.     int carry;
  34.     digit_t *presult = NULL;

  35.     pmul2 = mul2;
  36.     while (pmul2) {
  37.         carry = 0;
  38.         if (presult) {
  39.             presult = presult->next;
  40.             node = presult;
  41.         }
  42.         pmul1 = mul1;
  43.         while (pmul1) {
  44.             if (!node) {
  45.                 node = malloc(sizeof(digit_t));
  46.                 node->v = '0';
  47.                 node->next = NULL;
  48.                 if(pnode) {
  49.                     pnode->next = node;
  50.                     pnode = node;
  51.                 } else {
  52.                     pnode = node;
  53.                     result = node;
  54.                     presult = result;
  55.                 }
  56.             }

  57.             carry += (node->v-'0') + (pmul1->v - '0') * (pmul2->v - '0');
  58.             node->v = carry % 10 + '0';
  59.             carry /= 10;

  60.             node = node->next;
  61.             pmul1 = pmul1->next;
  62.         }
  63.         while (carry) {
  64.             if (!node) {
  65.                 node = malloc(sizeof(digit_t));
  66.                 node->v = '0';
  67.                 node->next = NULL;
  68.                 pnode->next = node;
  69.                 pnode = node;
  70.             }
  71.             carry += node->v - '0';
  72.             node->v = carry % 10 + '0';
  73.             carry /= 10;
  74.             node = node->next;
  75.         }

  76.         pmul2 = pmul2->next;
  77.     }

  78.     return result;
  79. }

  80. void free_digit(digit_t *p)
  81. {
  82.     digit_t *node=p;

  83.     while (node) {
  84.         digit_t *next = node->next;
  85.         free(node);
  86.         node = next;
  87.     }
  88. }

  89. /* invert output */
  90. void output_digit(digit_t *head)
  91. {
  92.     digit_t *node = head;
  93.     
  94.     if (!head)
  95.         return;

  96.     output_digit(head->next);
  97.     putchar(node->v);
  98. }

  99. #define N 100
  100. int main(int argc, const char *argv[])
  101. {
  102.     char buf[3];
  103.     int i;
  104.     digit_t *result = create_digit("1");
  105.     digit_t *n;
  106.     int sum = 0;

  107.     for (i=1; i<= N; i++) {
  108.         sprintf(buf, "%d", i);
  109.         n = create_digit(buf);
  110.         result = mul(result, n);
  111.         free_digit(n);
  112.     }

  113.     output_digit(result);

  114.     n = result;
  115.     while (n) {
  116.         sum += n->v - '0';
  117.         n = n->next;
  118.     }

  119.     printf("\nsum: %d\n", sum);
  120.         
  121.     return 0;
  122. }
任意精度的乘法。实现太别扭...

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

上一篇:euler22

下一篇:计算器bc

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