为什么我不能正确打印第47个斐波那契数字? [英] Why am i not able to print 47th fibonacci number correctly?

查看:201
本文介绍了为什么我不能正确打印第47个斐波那契数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用64位操作系统,那么我也无法正确打印小于40亿的第46个斐波那契数。

I am using 64 bit operating system ,then also i am not able to print 46th fibonacci number correctly which is less than 4 billion.

#include<cs50.h>
#include<stdio.h>

int main(void)
{  

    unsigned int n=50;
    int array[n];
    array[0]=0;
    array[1]=1;
    printf("%i\n",array[0]);
    printf("%i\n",array[1]);
    for(int i=2;i<n;i++)
    {
        array[i]=array[i-1]+array[i-2];
        printf("%i\n",array[i]);
    }


推荐答案

您必须使用long long作为数组的数据类型。因为您将要存储整数范围的超出范围的数字。(-2,147,483,648到2,147,483,647)
并且int的声明i应该在for循环之前。

You have to use long long as your data type of the array. because You are going to store out-range numbers of the integer range.(-2,147,483,648 to 2,147,483,647) And declaration of int i should be before the for loop.

#include<stdio.h>

int main(void)
{

    int n=50;
    long long array[n];
    array[0]=0;
    array[1]=1;
    printf("%lli\n",array[0]);
    printf("%lli\n",array[1]);
    int i;
    for(i=2;i<n;i++)
    {
        array[i]=array[i-1]+array[i-2];
        printf("%lli\n",array[i]);
    }
}

这篇关于为什么我不能正确打印第47个斐波那契数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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