#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* 大数加法:把两个数字字符串相加,返回结果(调用者 free) */
char *add_big(const char *x, const char *y)
{
int nx = strlen(x), ny = strlen(y);
int max = (nx > ny ? nx : ny) + 2;
char *r = malloc(max);
r[max - 1] = '\0';
int carry = 0, i = 0;
while (i < nx || i < ny || carry) {
int s = carry;
if (i < nx) s += x[nx - 1 - i] - '0';
if (i < ny) s += y[ny - 1 - i] - '0';
r[max - 2 - i] = (s % 10) + '0';
carry = s / 10;
i++;
}
/* 去掉前导 0 */
char *p = r + (max - 2 - i + 1);
if (*p == '0' && p[1]) p++;
memmove(r, p, strlen(p) + 1);
return r;
}
int main(void)
{
char *a = strdup("0"), *b = strdup("1");
int n = 100;
for (int i = 2; i <= n; i++) {
char *t = add_big(a, b);
free(a);
a = b; b = t;
}
printf("F(%d) = %s\n", n, b); // 354224848179261915075 ?
free(a); free(b);
return 0;
}