我做了一个程序,以查找一个数字是否属于斐波那契数列,如果它确实是它的位置。每当我输入一个数字if条件出错。斐波那契!如果一直都是假的
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main(void)
{
int i,x=1,y=1,z,num;
clrscr();
printf("Enter a number to find in fibonacci series:");
scanf("%d",&num);
/*to find if the number is a part of fibonacci series or not*/
if((isdigit(sqrt(5*num*num+4)))||(isdigit(sqrt(5*num*num-4)))) //<-- this if!
{//belongs to fibo!
for(i=1; ;i++)
{
if(x==num)
break;
z=x+y;
x=y;
y=z;
}
printf("%d is the %d term of fibonacci series.",num,i);
}
else
printf("Dear user,The entered number is not a part of the fibonacci series.");
getch();
}
isdigit检查给定的char是否是一个有效的ascii数字0-9。我不知道你在尝试使用它。你也真的需要在你的for循环上设置一个上限。 – Lazarus 2010-07-15 15:56:21
查看下面的评论 – 2010-07-15 16:01:41
@Lazarus:迂腐:平方根(一旦固定)的魔术已经在检查数字是否是斐波那契数。确定后,循环肯定会终止。虽然没有防守编码,但该程序在数学上听起来很合理。 – 2010-07-15 16:04:09