NBUT 1275-Play or Not Play【数学】 难度:**

题意:

To chat or not to chat, it is a question. Zero is so angry because when he plays LOL, there is nobody plays with him. So, zero wants to punish them.
Now, the rule is that there are almost N men, everybody can play with others. But, if one of them who plays with M people, these M people can not play with each other
Now we agreed that one plays with another to be a playing relationship.
For example, if man A plays with man B, man C, man D, then man B can not play with man C and man D, of course the same as man C and man D. And there are three playing relationships.
There are N people, can you get the maximum number of playing relationships.

输入
Input until EOF.
Each line contains one integer N(1 < N <= 10^9) means the number of people.
输出
One line one output, print the maximum number of playting relationships.
样例输入
2
3
4
样例输出
1
2
4

题解:

题意翻译过来就是:如果有两个点被连起来了,那么第三个点不能同时连接这两个点,也就是说整张图中找不到三个点满足互相都连接(或者说没有子三角形),问n个点最多能连接几个边。
首先我们考虑偶数个点的情况,比如四个点,很明显就只能连成一个四边形,即4条边,而对于六个点,可以连接出六边形后连接对角,即9条边,对于n个点(n为偶数)的情况,我们对于每一个点,都隔一个点连一下,所以对于每一个点都可以连接n/2个点,一共有n个点,所以可以连接n*(n/2)条边,但是因为每个点都被重复连了一次,所以还需要除以2,因此对于n个点可以连接出n×n/4条边。
对于奇数个点,比如五个点,其实本质上是四个点连接好以后,第五个孤点在连好的四点图中隔一个点连一个,因此是4×4/4+4/2=6条边,七个点就是6×6/4+6/2=12条边,因此n个点就是(n-1)×(n-1)/4+(n-1)/2=(n×n-1)/4条边。
结论:
n为偶数时,答案为n×n/4
n为奇数时,答案为(n×n-1)/4

代码:

#include<stdio.h>
int main()
{
	long long n;
	while(scanf("%lld",&n)!=EOF)
	n%2==0?
	printf("%lld\n",(n/2)*(n/2)):
	printf("%lld\n",((n-1)/2)*((n-1)/2)+((n-1)/2));
}

猜你喜欢

转载自blog.csdn.net/weixin_42921101/article/details/104322037