HDU5914 Triangle(思路)

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

思路

给了一个数n,代表有长度数字为从1~n的木头,问在这些木头中,最少删除几根木头,可以使剩下的木头不能组成三角形。

我们从三角形的定义入手,两边之和大于第三边,首先1 2 3不能组成三角形吗,所以先对小于3的n进行特判,然后定义两个指针从2 3开始,把它们标记并且把它们的和也标记,移动指针,到了最后,没有标记过的木头就是要删除的木头的数量

代码

#include
#include
#include
#include
#include
#include
#include
#include
#include
#define mem(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
#define mod 10000007
#define debug() puts("what the fuck!!!")
#define ll long long
using namespace std;
int vis[30];
int main()
{
    int t,n,q=1;
    scanf("%d",&t);
    while(t--)
    {
        mem(vis,0);
        scanf("%d",&n);
        printf("Case #%d: ",q++);
        if(n<=3)
        {
            printf("0\n");
            continue;
        }
        int t1=2,t2=3,sum=0;
        vis[1]=1;
        while(t2<=n)
        {
            int tmp=t1+t2;
            vis[t1]=1;
            vis[t2]=1;
            vis[tmp]=1;
            t1=t2;
            t2=tmp;
        }
        for(int i=1; i<=n; i++)
            if(vis[i])
                sum++;
        printf("%d\n",n-sum);
    }
    return 0;
}

你可能感兴趣的:(【思路,模拟,构造】)