3013
来源:互联网 发布:电台播音员自学软件 编辑:程序博客网 时间:2024/06/16 13:04
3013
Problem M
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 140 Accepted Submission(s) : 76
Problem Description
有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。请编程实现在第n年的时候,共有多少头母牛?
Input
输入数据由多个测试实例组成,每个测试实例占一行,包括一个整数n(0<n<55),n的含义如题目中描述。<br>n=0表示输入数据的结束,不做处理。
Output
对于每个测试实例,输出在第n年的时候母牛的数量。<br>每个输出占一行。
Sample Input
2450
Sample Output
246
题意:
有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。求第N年共有多少头牛?
思路:
每头母牛到第四年生小牛
f[i] = f[i-1] + f[i-3], i > 4
有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。求第N年共有多少头牛?
思路:
每头母牛到第四年生小牛
f[i] = f[i-1] + f[i-3], i > 4
AC代码:
#include<stdio.h>
int main()
{
int n,i;
int f1=1,f2=2,f3=3,f4;
while(scanf("%d",&n)!=EOF)
{if(n==0) return 0;
if(n>=1&&n<=3) f4=n;
if(n>3)
{
f1=1,f2=2,f3=3,f4=0;
for(i=3;i<n;i++)
{
f4=f1+f3;
f1=f2;
f2=f3;
f3=f4;
}
}
printf("%d\n",f4);
}
return 0;
}
int main()
{
int n,i;
int f1=1,f2=2,f3=3,f4;
while(scanf("%d",&n)!=EOF)
{if(n==0) return 0;
if(n>=1&&n<=3) f4=n;
if(n>3)
{
f1=1,f2=2,f3=3,f4=0;
for(i=3;i<n;i++)
{
f4=f1+f3;
f1=f2;
f2=f3;
f3=f4;
}
}
printf("%d\n",f4);
}
return 0;
}
0 0
- 3013
- pku 3013
- POJ 3013
- POJ 3013
- poj 3013
- poj 3013
- POJ 3013
- 3013 毕加猪
- POJ 3013 SPFA
- POJ 3013 SPFA
- BNUOJ 3013 Ancient Keyboard
- poj 3013 单源最短路
- poj 3013 SPFA
- 我的3013
- POJ 3013 Dijkstra
- 3013-3月英语学习
- Poj 3013 Big Christmas Tree
- poj 3013 big christmas tree
- Unicode、UTF-8 和 ISO8859-1 区别
- UE4下实现Vive手柄拾取物品
- Java NIO
- PHP下C扩展开发
- Android SurfaceView学习
- 3013
- Android ANR发生原因总结(来自本人cnblogs博客)
- Hibernate 中 的 SessionFactory 接口
- 重复new对象所带来的问题和解决办法
- 练习三 1012
- 这几年工作的总结
- scala简介
- Realm的翻译
- Android异步消息处理之Looper,Handler,Message,MessageQueue