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
  • 相关阅读:
    十度好友问题
    TCP传输连接建立与释放详解
    Android多点触控技术实战,自由地对图片进行缩放和移动
    如何判断一个变量是数组Array类型
    HDU 4725 The Shortest Path in Nya Graph-【SPFA最短路】
    解决外贸电商难题,PayPal中国外贸电商大会圆满礼成
    动物-蛇:家蛇
    动物-蛇:水蛇
    动物-鱼:河蟹
    动物-鱼:河虾
  • 原文地址:https://www.cnblogs.com/wenzhixin/p/7513027.html
Copyright © 2011-2022 走看看