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

全部博文(47)

文章存档

2014年(1)

2013年(7)

2012年(1)

2011年(38)

分类: C/C++

2011-03-15 22:57:45

The Fibonacci sequence is defined by the recurrence relation:

    Fn = Fn−1 + Fn−2, where F1 = 1 and F2 = 1.

Hence the first 12 terms will be:

    F1 = 1
    F2 = 1
    F3 = 2
    F4 = 3
    F5 = 5
    F6 = 8
    F7 = 13
    F8 = 21
    F9 = 34
    F10 = 55
    F11 = 89
    F12 = 144

The 12th term, F12, is the first term to contain three digits.

What is the first term in the Fibonacci sequence to contain 1000 digits?
--------------------
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.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 *add(digit_t *m, digit_t *n)
  30. {
  31.     digit_t *result;
  32.     digit_t *node;
  33.     digit_t *prev = NULL;
  34.     int carry = 0;

  35.     while (m && n) {
  36.         node = malloc(sizeof(*node));
  37.         node->next = NULL;
  38.         node->v = '0';
  39.         if (prev) {
  40.             prev->next = node;
  41.             prev = node;
  42.         } else {
  43.             result = node;
  44.             prev = node;
  45.         }

  46.         carry += (m->v - '0') +(n->v - '0');
  47.         node->v = carry % 10 + '0';
  48.         carry /= 10;

  49.         m = m->next;
  50.         n = n->next;
  51.     }

  52.     while (m) {
  53.         node = malloc(sizeof(*node));
  54.         node->next = NULL;
  55.         node->v = '0';
  56.         if (prev) {
  57.             prev->next = node;
  58.             prev = node;
  59.         } else {
  60.             result = node;
  61.             prev = node;
  62.         }
  63.         carry += m->v - '0';
  64.         node->v = '0' + carry % 10;
  65.         carry /= 10;

  66.         m = m->next;
  67.     }

  68.     while (n) {
  69.         node = malloc(sizeof(*node));
  70.         node->next = NULL;
  71.         node->v = '0';
  72.         if (prev) {
  73.             prev->next = node;
  74.             prev = node;
  75.         } else {
  76.             result = node;
  77.             prev = node;
  78.         }
  79.         carry += n->v - '0';
  80.         node->v = carry % 10 + '0';
  81.         carry /= 10;

  82.         n = n->next;
  83.     }

  84.     while (carry) {
  85.         node = malloc(sizeof(*node));
  86.         node->next = NULL;
  87.         node->v = '0';
  88.         if (prev) {
  89.             prev->next = node;
  90.             prev = node;
  91.         } else {
  92.             result = node;
  93.             prev = node;
  94.         }

  95.         node->v = carry % 10 + '0';
  96.         carry /= 10;
  97.     }

  98.     return result;
  99. }

  100. /* invert output */
  101. void output_digit(digit_t *head)
  102. {
  103.     digit_t *node = head;
  104.     
  105.     if (!head)
  106.         return;

  107.     output_digit(head->next);
  108.     putchar(node->v);
  109. }

  110. void free_digit(digit_t *p)
  111. {
  112.     digit_t *node=p;

  113.     while (node) {
  114.         digit_t *next = node->next;
  115.         free(node);
  116.         node = next;
  117.     }
  118. }

  119. int check(digit_t *n)
  120. {
  121.     int num = 0;

  122.     output_digit(n);
  123.     printf("\n");
  124.     while (n) {
  125.         num++;
  126.         n = n->next;
  127.     }

  128.     return num == 1000;
  129. }

  130. int main(int argc, const char *argv[])
  131. {
  132.     digit_t *n, *m, *sum;
  133.     int result;

  134.     n = create_digit("1");
  135.     m = create_digit("1");
  136.     result = 2;
  137.     while (1) {
  138.         result++;
  139.         sum = add(n, m);
  140.         if (check(sum)) break;

  141.         free_digit(n);
  142.         n = m;
  143.         m = sum;
  144.     }

  145.     printf("result: %d\n", result);

  146.     return 0;
  147. }

  148. 任意精度的加法。


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

上一篇:计算器bc

下一篇:效率版strlen

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