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 (T≤20), which indicates the number of test cases.

For each test case, there is only one line describing the given integer n (1≤n≤20).

 
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(1≤n≤20),代表有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代码:
 #include<stdio.h>
int main()
{
//注意首位多存一个0
int map[]={,,,,,,,,,,,,,,,,,,,,};
int n,t=,T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
printf("Case #%d: %d\n",t++,map[n]);
}
return ;
}
 
 
 
Recommend
上一篇:ORACLE 归档日志打开关闭方法


下一篇:Windows 7 OpenGL配置