斐波那契数列某数的序号-C语言
作者:互联网
给定一个正整数,求得该数在斐波那契数列中的序号,若不存在,则返回-1
C代码:
int main()
{
int fib1 = 0, fib2 = 1, i = 2;
int target;
scanf("%d", &target);
while(fib2 <= target){
fib1 = fib1 + fib2;
++i;
swap(fib1, fib2);
}
if(fib1 == target)
printf("%d", i - 1);
else
printf("-1");
}
标签:某数,target,int,fib1,fib2,斐波,printf,C语言 来源: https://blog.csdn.net/weixin_47406676/article/details/120274146