A - Pen Counts HDU - 4486(数学题)

43 篇文章 0 订阅
20 篇文章 0 订阅

Chicken farmer Xiaoyan is getting three new chickens, Lucy, Charlie and CC. She wants to build a chicken pen so that each chicken has its own, unobstructed view of the countryside. The pen will have three straight sides; this will give each chicken its own side so it can pace back and forth without interfering with the other chickens. Xiaoyan finds a roll of chicken wire (fencing) in the barn that is exactly N feet long. She wants to figure out how many different ways she can make a three sided chicken pen such that each side is an integral number of feet, and she uses the entire roll of fence.
Different rotations of the same pen are the same, however, reflections of a pen may be different (see below).


Input
The first line of input contains a single integer P,(1<= P <=1000), which is the number of data sets that follow. Each data set should be processed identically and independently.

Each data set consists of a single line of input. It contains the data set number, K, and the length of the roll of fence, N, (3 <= N <= 10000).
Output
For each data set there is a single line of output. It contains the data set number, K, followed by a single space which is then followed by an integer which is the total number of different three-sided chicken pen configurations that can be made using the entire roll of fence.


开始题目没有看懂,他说的是,给你一个三角形的边长的总和,然后问一共可以构成多少种三角形,三角形经过旋转之后不能相同的视为两种,(也就是说一组数据一般有两种,但是等边三角形和等腰三角形除外算一种)


寻找枚举界限

思路:
先总结一下三角形的相关知识。
设三角形周长为len。三边关系为l3>=l2>=l1。
两边之和>第三边。
所以三角形中最长边的取值范围为
                len/3向上取整<=l3<len/2向上取整。
rest=len-l3。那么第二大边取值范围为
                rest/2向下取整<l2<=l3。
对于一个等腰三角形其底边长的最大值<(len+1)/2。所以给定一个周长可以确定有多少个等腰或等边三角形。
所以每确定一个最长边那么三角形的个数是能确定的。

#include <iostream>
#include<cstring>
#include<cstdio>
using namespace std;

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        int n,m;
        cin>>n>>m;
        int ans=0;
        for(int l3=(m+2)/3 ; l3<(m+1)/2 ;l3++)  //枚举最大边长
        {
            int res=m-l3;
            int okl2=l3-(res-1)/2;  //  第二长边长的可取值
            ans+=okl2*2;   // 每种可以生成两种
            if(res%2==0)  // 如果是剩余的可为等腰三角形 需要减去
                ans--;
            ans--;   //l3 与l2相等的情况减去(不能算两次)
        }
        if(m%3==0)  //  如果是等边三角形,在开始部门全部减去了这种情况 ,所以需要加一
            ans++;
        cout<<n<<" "<<ans<<endl;
    }

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值