问题 : 来简单地数个数
时间限制: 1 Sec 内存限制: 128 MB题目描述
f1 = 1
f2 = 2
fn = fn-1 + fn-2 (n>=3)
蔡老板想知道,给你两个数 a、b,你能否求出在区间[a,b]里有多少个斐波那契数。
输入
输出
样例输入
10 100
1234567890 9876543210
0 0
样例输出
5
4
#include <stdio.h>
#include <string.h>
int cmp(char *a, char *b)
{
int lena, lenb;
lena = strlen(a);
lenb = strlen(b);
if (lena > lenb)
return 1;
if (lena < lenb)
return 0;
for (int i = 0; i < lena; i++)
{
if (a[i] > b[i])
return 1;
if (a[i] < b[i])
return 0;
}
return 1;
}
int main()
{
char fib[500][110], a[110], b[110];
int s[500][110], i, j, c, p, k, t = 1;
strcpy(fib[1], "1");
strcpy(fib[2], "2");
memset(s, 0, sizeof(s));
s[1][0] = 1;
s[2][0] = 2;
for (i = 3; i < 500; i++)
{
c = 0;
for (j = 0; j < t; j++)
{
s[i][j] = s[i-1][j] + s[i-2][j] + c;
c = s[i][j] / 10;
s[i][j] %= 10;
}
if (c)
s[i][t++] = c;
p = t - 1;
k = 0;
while (p >= 0)
fib[i][k++] = s[i][p--] + '0';
fib[i][k] = '\0';
}
while (scanf("%s%s", a, b), a[0] - '0' || b[0] - '0')
{
int sum = 0;
for (i = 1; i < 500; i++)
{
if (cmp(fib[i], a) && cmp(b, fib[i]))
sum++;
if (!cmp(b, fib[i]))
break;
}
printf("%d\n", sum);
}
return 0;
}