TOJ 3845.Cut Stick(斐波那契)

题目链接:http://acm.tju.edu.cn/toj/showp3845.html


3845.    Cut Stick
Time Limit: 1.0 Seconds    Memory Limit: 65536K
Total Runs: 546    Accepted Runs: 259



Ann meets another problem.

Since she has a n-meter-long stick, she wants to cut it into small ones. Of course she would not let the small sticks shorter than 1 meter. At the same time she is so mean that she can't stand that any three of the small sticks could spell a triangle. That is to say, any 3 of the small sticks can not form a triangle if there are 3 or more small sticks.

You can simple assume that the small sticks only have integer length.

Input

A line contains a positive integer n, which is the length of the long sticks. It does not exceed 10^5.

Output

Print a number -- the maximum number of small sticks Ann could get.

Sample Input

1
2
3
4

Sample Output

1
2
2
3



Source: TJU 2012 Team Selection
Submit   List    Runs   Forum   Statistics


剪棍子,在满足任意三根木棍不能构成三角形的前提下问最多可以剪成几根。众所周知,构成三角形的条件是任意两边的长度要大于第三边的长度,因此为了尽可能多的剪,所以从1开始,每次剪棍子都要满足不大于(也就是等于)前边两次剪得长度和:


#include <stdio.h>
using namespace std;
int main(){
	int l;	
	while(~scanf("%d",&l)){
		int a=0,b=1,m;
		for(m=1;;m++){
			l-=b;
			if(a+b>l)
				break;
			int tmp=a;
			a=b;
			b+=tmp;
		}
		printf("%d\n",m);
	}
} 


按此思路写完才发现,这不就是斐波那契数列么,于是悲伤地用斐波那契重写了一遍。。。


#include <stdio.h>
int febo[100001]={1,1};
int main(){
	for(int i=2;i<100001;i++)
		febo[i]=febo[i-1]+febo[i-2];
	int l,m;
	while(~scanf("%d",&l)){
		m=0;
		while(l>=febo[m])
			l-=febo[m++];
		printf("%d\n",m);
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值