杭电oj-1316-How Many Fibs?
来源:互联网 发布:记事本写c语言 编辑:程序博客网 时间:2024/06/05 16:17
Problem Description
Recall the definition of the Fibonacci numbers:
f1 := 1
f2 := 2
fn := fn-1 + fn-2 (n >= 3)
Given two numbers a and b, calculate how many Fibonacci numbers are in the range [a, b].
Input
The input contains several test cases. Each test case consists of two non-negative integer numbers a and b. Input is terminated by a = b = 0. Otherwise, a <= b <= 10^100. The numbers a and b are given with no superfluous leading zeros.
Output
For each test case output on a single line the number of Fibonacci numbers fi with a <= fi <= b.
Sample Input
10 100
1234567890 9876543210
0 0
Sample Output
5
4
题目意思是给你两个数,让你输出两个数(闭区间)之间的菲波那切数有多少个,当输入0 0时结束。
思路:这是一道典型的打表查询题,使用大数相加算出1-10^100之内的菲波那切数存起来就行了,然后查询
菲波那切数列:f[n]=f[n-1]+f[n-2]
代码如下:
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
using namespace std;
char f[1100][550];
int f1[550],f2[550];
char a[550],b[550];
void feibo()
{
strcpy(f[0],"1");
strcpy(f[1],"2");
int i,j,k;
for(i=2;i<1000;i++)
{
int l1=strlen(f[i-2]);
int l2=strlen(f[i-1]);
memset(f1,0,sizeof(f1));
memset(f2,0,sizeof(f2));
for(j=l1-1;j>=0;j--)
f1[j]=f[i-2][l1-j-1]-'0';
for(j=l2-1;j>=0;j--)
f2[j]=f[i-1][l2-j-1]-'0';
int l;
int jinwei=0;
if(l1>l2)
l=l1;
else
l=l2;
while(j<=l)
{
f1[j]=f1[j]+f2[j]+jinwei;
if(f1[j]>9)
{
f1[j]-=10;
jinwei=1;
}
else
jinwei=0;
j++;
}
while(f1[l]==0)
l--;
for(j=l;j>=0;j--)
f[i][j]=f1[l-j]+'0';
}
return;
}
int main()
{
feibo();
while(scanf("%s",a)&&scanf("%s",b))
{
if(a[0]=='0'&&b[0]=='0')
break;
int l1,l2,l;
l1=strlen(a);
l2=strlen(b);
int sum=0;
for(int i=0;i<1000;i++)
{
l=strlen(f[i]);
if(l1==l2)
{
if(l==l1)
if((strcmp(f[i],a)==0||strcmp(f[i],a)>0)&&(strcmp(f[i],b)<0||strcmp(f[i],b)==0))
sum++;
}
if(l1!=l2)
{
if(l1==l)
if(strcmp(f[i],a)==0||strcmp(f[i],a)>0)
sum++;
if(l1<l&&l<l2)
sum++;
if(l==l2)
if(strcmp(f[i],b)<0||strcmp(f[i],b)==0)
sum++;
}
if(l>l2)
break;
}
printf("%d\n",sum);
}
return 0;
}
Recall the definition of the Fibonacci numbers:
f1 := 1
f2 := 2
fn := fn-1 + fn-2 (n >= 3)
Given two numbers a and b, calculate how many Fibonacci numbers are in the range [a, b].
Input
The input contains several test cases. Each test case consists of two non-negative integer numbers a and b. Input is terminated by a = b = 0. Otherwise, a <= b <= 10^100. The numbers a and b are given with no superfluous leading zeros.
Output
For each test case output on a single line the number of Fibonacci numbers fi with a <= fi <= b.
Sample Input
10 100
1234567890 9876543210
0 0
Sample Output
5
4
题目意思是给你两个数,让你输出两个数(闭区间)之间的菲波那切数有多少个,当输入0 0时结束。
思路:这是一道典型的打表查询题,使用大数相加算出1-10^100之内的菲波那切数存起来就行了,然后查询
菲波那切数列:f[n]=f[n-1]+f[n-2]
代码如下:
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
using namespace std;
char f[1100][550];
int f1[550],f2[550];
char a[550],b[550];
void feibo()
{
strcpy(f[0],"1");
strcpy(f[1],"2");
int i,j,k;
for(i=2;i<1000;i++)
{
int l1=strlen(f[i-2]);
int l2=strlen(f[i-1]);
memset(f1,0,sizeof(f1));
memset(f2,0,sizeof(f2));
for(j=l1-1;j>=0;j--)
f1[j]=f[i-2][l1-j-1]-'0';
for(j=l2-1;j>=0;j--)
f2[j]=f[i-1][l2-j-1]-'0';
int l;
int jinwei=0;
if(l1>l2)
l=l1;
else
l=l2;
while(j<=l)
{
f1[j]=f1[j]+f2[j]+jinwei;
if(f1[j]>9)
{
f1[j]-=10;
jinwei=1;
}
else
jinwei=0;
j++;
}
while(f1[l]==0)
l--;
for(j=l;j>=0;j--)
f[i][j]=f1[l-j]+'0';
}
return;
}
int main()
{
feibo();
while(scanf("%s",a)&&scanf("%s",b))
{
if(a[0]=='0'&&b[0]=='0')
break;
int l1,l2,l;
l1=strlen(a);
l2=strlen(b);
int sum=0;
for(int i=0;i<1000;i++)
{
l=strlen(f[i]);
if(l1==l2)
{
if(l==l1)
if((strcmp(f[i],a)==0||strcmp(f[i],a)>0)&&(strcmp(f[i],b)<0||strcmp(f[i],b)==0))
sum++;
}
if(l1!=l2)
{
if(l1==l)
if(strcmp(f[i],a)==0||strcmp(f[i],a)>0)
sum++;
if(l1<l&&l<l2)
sum++;
if(l==l2)
if(strcmp(f[i],b)<0||strcmp(f[i],b)==0)
sum++;
}
if(l>l2)
break;
}
printf("%d\n",sum);
}
return 0;
}
0 0
- 杭电oj-1316-How Many Fibs?
- 杭电1316(How Many Fibs?)
- 杭电OJ(HDOJ)1316题:How many Fibs?(大数操作——比较)
- CUMT OJ 1021 How Many Fibs?
- POJ 2413 How many Fibs? /HDOJ 1316 How Many Fibs?
- HDU 1316 How Many Fibs?
- HDU 1316 How Many Fibs?
- HDOJ 1316 How Many Fibs?
- HDU 1316 How Many Fibs?
- hdu 1316 How Many Fibs?
- HDOJ 1316 How Many Fibs?
- hdu 1316 How Many Fibs?
- HDUJ 1316 How Many Fibs?
- HDU 1316 How Many Fibs?
- HDUOJ - 1316 How Many Fibs?
- hdu 1316 How Many Fibs?
- hdu 1316 How Many Fibs?
- hdu 1316 How Many Fibs?
- 计算机图形学(一) 视频显示设备_5_平板显示器
- 《算法(第四版)》排序-----堆排序
- imageloder显示参数配置
- mybatis连载(2)
- Ivan and Powers of Two
- 杭电oj-1316-How Many Fibs?
- 插入排序
- 创建和引用单链表
- POJ 2699 The Maximum Number of Strong Kings 最大流 枚举
- java.awt.headless 详解
- SharedPreference.Editor的apply和commit方法异同
- MySQL之DDL,DQL,DML,DCL
- 用友U8数据库修复用友t3数据库修复用友/823错误824错误/检测到基于一致性的逻辑 I O 错误/ tempdb 空间用尽或某一系统表不一致
- C++ list::insert