当我使用长整型时,我的代码不起作用,它与 int 一起工作得很好

My code doesn't work when I use long long int and it works perfectly fine with int

本文关键字:int 它与 一起 工作 很好 不起作用 我的 长整型 代码      更新时间:2023-10-16
#include<iostream>
using namespace std;
int main(){
int m,a[m],i,current; // on using long long int here
cin>>m;
a[0]=0; 
a[1]=1; 
current=1;
for(i=2;i<=m;i++){
a[i]=a[i-1]+a[i-2];
a[i]=a[i]%10;   //taking last digit of the fibonacci sequence
current=current+a[i]; //summing
current=current%10;
cout<<"current="<<current<<"n";
}
cout<<"Last Digit of the sum upto "<<m<<"th Fibonacci number is "<<current;

}

当我使用长整型时,我的代码不起作用,它与 int 一起工作得很好。

int m,a[m]

这是:

a( 未定义的行为(在初始化之前使用 m(

b( 依赖于非标准的 GCC 扩展(可变长度 C 样式数组(

你很"幸运",它适用于int