【发布时间】:2023-03-04 13:10:01
【问题描述】:
for(int a0 = 0; a0 < t; a0++){
long long int n;
//taking the input limit
cin >> n;
long long int n1=8,n2=34,sum=10;
//iteration till the limit
while(n2<=n)
{
long long int l=n2;
//for finding the next even fibonacci number
n2=4*(n2)+n1;
n1=l;
sum+=n1;
}
cout<<sum<<endl;
}
这是在给定限制 n 的情况下求偶数斐波那契数之和的代码。 当我使用 int 代替 long long int 时出现超时问题。处理不同的数据类型有区别吗?性能会有怎样的变化?
【问题讨论】:
-
你很可能会溢出
int,因此永远不会跳出循环。 -
请提供
n的测试值 -
你能解释一下吗??溢出时会发生什么?
标签: c++ arrays performance