zoukankan      html  css  js  c++  java
  • HDU 5914 Triangle(打表——斐波那契数的应用)

    题目链接:

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

    Problem Description
    Mr. Frog has n sticks, whose lengths are 1,2, 3n 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
    ",t++,map[n]);
    12     }
    13     return 0;
    14 }
     
     
     
    Recommend
  • 相关阅读:
    移动网页如何只调出数字键盘
    把HTML5网页封装成APP,APK的方法
    HTML5,微信开发原码社区
    display:table 水平居中
    input在苹果浏览器下变成圆角的解决方案
    四种方法解决DIV高度自适应问题
    jquery prop和attr的区别
    移动端网页JS框架-手机触摸事件框架,日历框架带滑动效果
    meta viewport标签的使用说明(手机浏览缩放控制)
    javascript json格式解析方法
  • 原文地址:https://www.cnblogs.com/wenzhixin/p/7513027.html
Copyright © 2011-2022 走看看