[C] Beer Land

Beer Land

Time Limit: 1 Sec Memory Limit: 64 MB

Description

King ACM 2002 received some clear ground and some money through inheritance. Terms of the will and testament require the heir to build n towns. He was also required to build the least quantity of two-way roads between the towns such that one could travel from any town to any other town even if one road was closed under repair. Carefully, all such roads must be constructed as geometrically straight lines. Further, the condition that a traveller be able to get from one town to another (even with one road closed) needs to be satisfied by traversing the roads as straight lines in their entirety — not stopping partway along one road to switch to another. Because of rich soil in those lands, the future inhabitants of the place have decided to produce beer. On the crossroads (a crossroad is merey the intersection of two or more roads) the king plans to build beer stands (one stand per a crossroad). So, if three or four (or twenty) roads intersect in a single point, there’s still only one appropriate place for a beer stand. The King must choose locations for the towns and the roads. You need to advise him so that he builds as many beer stands as possible.

Input

The first line of input contains the number of test cases. For each case there is a line containing a single integer n (1 ≤ n ≤ 32767). This represents the number of towns to be built.

Output

For each n, print a line containing the maximum number of beer stands possible to build.

Sample Input

3
3
4
5

Sample Output

0
1
5

[做本题体会]

我认为本题的突破点为找到其中所蕴含的规律。(找到点数与交点之间的规律)
下图为我找规律的分析过程(啊!字有点丑,哈哈哈~):
在这里插入图片描述
在这里插入图片描述
找到规律后,代码的编写就变得简单了。
以下为我的代码:

#include <stdio.h>

int main() {
//   测试组数
    int n_group;
    scanf("%d",&n_group);
    int count,test;
    for (count=1; count<=n_group;count++) {
        scanf("%d",&test);
        if(test<=3)
        {
            printf("%d\n",0);
        }
        if (test>3) {
        if (test%2!=0) {
            printf("%d\n",test*(test-3)/2);
        }
        else
        {
            printf("%d\n",test*(test-4)/2+1);
//          printf("%d\n",(2*(test-3)+(test-4)*(test-2))/2);
        }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值