Fibonacci Again解题报告
2013-07-29 19:41
183 查看
题目摘要:Thereare another kind of Fibonacci numbers: F(0) = 7, F(1) = 11, F(n) = F(n-1) +F(n-2) (n>=2).
题目大意:另外一种斐波那契数列,第一项为7,第二项为11。
输入输出要求:
Input
Input consists of a sequence of lines, eachcontaining an integer n. (n < 1,000,000).
Output
Print the word "yes" if 3 divideevenly into F(n).
Print the word "no" if not.
输入输出样例:
Sample Input
0
1
2
3
4
5
Sample Output
no
no
yes
no
no
no
解题思路:咳咳,找规律,8个一循环。
代码:
#include<iostream>
using namespace std;
long long f(int n)
{
long long a=7;
long long b=11;
long long c;
int i;
if(n==0)
return 7;
else if(n==1)
return 11;
else
{
for(i=2;i<=n;i++)
{
c=a+b;
a=b;
b=c;
}
}
return b;
}
int main()
{
int n;
while(cin>>n)
{
n%=8;
if(f(n)%3==0)
cout<<"yes"<<endl;
else
cout<<"no"<<endl;
}
return 0;
}
解题感想:最先想到直接用递归求数列,结果发现n能取100万,递归肯定递不出来,所以用了另一种方法求f(n),开一个循环就好。交了代码WA掉,怎么也没想明白错在哪,后来将f(n)输出看看,发现n去90以后结果就变负值了,原因是long long也不够存f(n)的值。这样的话只能再换思路了,因为题中是对3取余,所以发现结果是8个一循环,将n对8取余一下就好了。
题目大意:另外一种斐波那契数列,第一项为7,第二项为11。
输入输出要求:
Input
Input consists of a sequence of lines, eachcontaining an integer n. (n < 1,000,000).
Output
Print the word "yes" if 3 divideevenly into F(n).
Print the word "no" if not.
输入输出样例:
Sample Input
0
1
2
3
4
5
Sample Output
no
no
yes
no
no
no
解题思路:咳咳,找规律,8个一循环。
代码:
#include<iostream>
using namespace std;
long long f(int n)
{
long long a=7;
long long b=11;
long long c;
int i;
if(n==0)
return 7;
else if(n==1)
return 11;
else
{
for(i=2;i<=n;i++)
{
c=a+b;
a=b;
b=c;
}
}
return b;
}
int main()
{
int n;
while(cin>>n)
{
n%=8;
if(f(n)%3==0)
cout<<"yes"<<endl;
else
cout<<"no"<<endl;
}
return 0;
}
解题感想:最先想到直接用递归求数列,结果发现n能取100万,递归肯定递不出来,所以用了另一种方法求f(n),开一个循环就好。交了代码WA掉,怎么也没想明白错在哪,后来将f(n)输出看看,发现n去90以后结果就变负值了,原因是long long也不够存f(n)的值。这样的话只能再换思路了,因为题中是对3取余,所以发现结果是8个一循环,将n对8取余一下就好了。
相关文章推荐
- YT02-简单数学课堂题-1004 Fibonacci Again -(5.31日-烟台大学ACM预备队解题报告)
- HDOJ 1021 Fibonacci Again解题报告
- HDOJ 4006The kth great number 解题报告
- codevs 1432 总数统计 二分 解题报告
- TYVJ 1093 验证数独 解题报告
- POJ 2853 解题报告
- BZOJ 1097 [POI 2007] SPFA+状压DP 解题报告
- URAL 1348 Goat in the Garden 2计算几何(解题报告)
- Codeforces Round #342 (Div. 2) 解题报告
- ACM POJ 2245Lotto解题报告
- 九度的《剑指Offer:名企面试官精讲典型编程题》 面试题 解题报告
- HDU 1796 How many integers can you find 解题报告(数论)
- POJ1426 Find The Multiple 解题报告
- HDOJ 4009-Transfer water最小树形图解题报告
- PAT 解题报告 1048. Find Coins (25)
- [leetcode] 311. Sparse Matrix Multiplication 解题报告
- 1008. Airline Routes (35)解题报告
- 2012 ACM/ICPC Asia Regional Changchun Online 解题报告
- Google Code jam Qualification Round 2015 解题报告
- [leetcode] 281. Zigzag Iterator 解题报告