用C ++或C语言打印fibo大数字 [英] print fibo big numbers in c++ or c language

查看:113
本文介绍了用C ++或C语言打印fibo大数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用递归代码编写了show fibonacci系列的代码,但是对于n> 43却无法正确显示(例如:对于n = 100 show:-980107325).

I write this code for show fibonacci series using recursion.But It not show correctly for n>43 (ex: for n=100 show:-980107325).

#include<stdio.h>
#include<conio.h>

void fibonacciSeries(int);

void fibonacciSeries(int n)
{
static long d = 0, e = 1;
long c;
if (n>1)
{
    c = d + e;
    d = e;
    e = c;
    printf("%d \n", c);
    fibonacciSeries(n - 1);
}
}

int main()
{
long a, n;
long long i = 0, j = 1, f;
printf("How many number you want to print in the fibonnaci series :\n");
scanf("%d", &n);

printf("\nFibonacci Series: ");
printf("%d", 0);
fibonacciSeries(n);
_getch();
return 0;
}

推荐答案

fib(100)的值太大,甚至会溢出64位数字.要对如此大的值进行运算,您需要执行任意精度算术. C或C ++标准库都不提供任意精度算术,因此您需要自己实现它或使用其他人编写的库.

The value of fib(100) is so large that it will overflow even a 64 bit number. To operate on such large values, you need to do arbitrary-precision arithmetic. Arbitrary-precision arithmetic is not provided by C nor C++ standard libraries, so you'll need to either implement it yourself or use a library written by someone else.

对于适合您long long的较小值,您的问题是您使用了错误的printf格式说明符.要打印long long,您需要使用%lld.

For smaller values that do fit your long long, your problem is that you use the wrong printf format specifier. To print a long long, you need to use %lld.

这篇关于用C ++或C语言打印fibo大数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆