A

来源:互联网 发布:法语之言能无从乎 编辑:程序博客网 时间:2024/05/16 15:20

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
原创粉丝点击