题解 | #Fibonacci数列#

Fibonacci数列

http://www.nowcoder.com/questionTerminal/18ecd0ecf5ef4fe9ba3f17f8d00d2d66

#include <math.h>
#include <stdio.h>
int main()
{
	int fib1 = 0;
	int fib2 = 1;
	int fib3 = 1;
	int n = 0;
	scanf("%d", &n);
	while (1)
	{
		if (n == fib2)
		{
			printf("0\n");
			break;
		}
		else if (n < fib2)
		{
			if (abs(fib1 - n) < abs(fib2 - n))
			{
				printf("%d\n", abs(fib1 - n));

			}
			else
			{
				printf("%d", abs(fib2 - n));
			}
			break;
		}
		fib1 = fib2;
		fib2 = fib3;
		fib3 = fib1 + fib2;
	}
	return 0;
}
全部评论

相关推荐

11-08 17:36
诺瓦科技_HR
点赞 评论 收藏
分享
点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务