HDU 5914 Triangle(打表——斐波那契数的应用)

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5914

Problem Description
Mr. Frog has n sticks, whose lengths are 1,2, 3 n respectively. Wallice is a bad man, so he does not want Mr. Frog to form a triangle with three of the sticks here. He decides to steal some sticks! Output the minimal number of sticks he should steal so that Mr. Frog cannot form a triangle with
any three of the remaining sticks.
 
Input
The first line contains only one integer T ( T20), which indicates the number of test cases. 

For each test case, there is only one line describing the given integer n (1n20).
 
Output
For each test case, output one line “Case #x: y”, where x is the case number (starting from 1), y is the minimal number of sticks Wallice should steal.
 
Sample Input
3
4
5
6
 
Sample Output
Case #1: 1
Case #2: 1
Case #3: 2
 
Source
题意描述:
输入数字n( 1n20),代表有n根长度为1,2,3...到n的木棒
计算并输出至少去掉几根木棒使得剩下的木棒不能构成任何一个三角形
解题思路:
先说结论:在保证不构成三角形的情况下,尽量取程度短的木棒。
因为在取舍时,较短木棒相比较长木棒更不容易构成三角形,但是选择短的时候不能和之前确定长度的木棒构成三角形。
那么取木棒时,首先1,2,3必取(既不构成三角形也最短),4不能取(与2,3构成三角形),5(取,相对比5长的木棒,5最不容易构成三角形,且与之前的木棒均构不成三角形),6(不取,与3和5构成三角形)...依次取得可知,n根木棒(从3根起),留下出的数构成斐波那契数1,2,3,5,8,13,14(很神奇有木有),那么只需要数一下,1到n有几个非斐波那契数即可。
故打表int map[21]={0,0,0,0,1,1,2,3,3,4,5,6,7,7,8,9,10,11,12,13,14};//首位存个0
AC代码:
 1 #include<stdio.h>
 2 int main()
 3 {
 4     //注意首位多存一个0 
 5     int map[21]={0,0,0,0,1,1,2,3,3,4,5,6,7,7,8,9,10,11,12,13,14};
 6     int n,t=1,T;
 7     scanf("%d",&T);
 8     while(T--)
 9     {
10         scanf("%d",&n);
11         printf("Case #%d: %d\n",t++,map[n]);
12     }
13     return 0;
14 }

 

 
 
 

 

Recommend

转载于:https://www.cnblogs.com/wenzhixin/p/7513027.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值