分析
顺时针给一六边形的各边长,问该六边形有几个边长为1的小三角形。
首先考察是否有规律可循,显然,变量太多,直接通过边长考察个数可能不好。
其实可以间接地通过剥洋葱
的方法一层一层剥过去。边长为n的边,可以为这个六边形贡献2n-1个小三角形,再向内缩短边长形成一个六边形直至无法形成六边形,例如线、三角形、菱形再特判。但是这样也较为麻烦。
既然通过边长数这个方向不好,那么可以尝试通过面积,每一个小三角形的面积是固定的,我们只需要计算出该六边形的面积,将其除以小三角形的面积即可得到个数。
如何计算六边形的面积?可以尝试割补法。任取六边形连续的三个边长,将其展开成一条直线,即为补成一正三角形的边长。证明正三角形略。
不妨取
a[0],a[1],a[2]
三条边长,和记为
A
,那么该正三角形的面积为
那么顺时针方向需要割掉的三角形是以 a[0],a[2],a[4] 为底的三角形,其面积为
所以该六边形的面积为
那么个数 n 为
代码
#include <cstdio>
int main()
{
int a[6];
for (int i = 0; i < 6; i++) scanf("%d", &a[i]);
printf("%d\n", (a[0]+a[1]+a[2])*(a[0]+a[1]+a[2])-a[0]*a[0]-a[2]*a[2]-a[4]*a[4]);
return 0;
}
题目
Description
Gerald got a very curious hexagon for his birthday. The boy found out that all the angles of the hexagon are equal to . Then he measured the length of its sides, and found that each of them is equal to an integer number of centimeters. There the properties of the hexagon ended and Gerald decided to draw on it.
He painted a few lines, parallel to the sides of the hexagon. The lines split the hexagon into regular triangles with sides of 1 centimeter. Now Gerald wonders how many triangles he has got. But there were so many of them that Gerald lost the track of his counting. Help the boy count the triangles.
Input
The first and the single line of the input contains 6 space-separated integers a1, a2, a3, a4, a5 and a6(1 ≤ ai ≤ 1000) — the lengths of the sides of the hexagons in centimeters in the clockwise order. It is guaranteed that the hexagon with the indicated properties and the exactly such sides exists.
Output
Print a single integer — the number of triangles with the sides of one 1 centimeter, into which the hexagon is split.
Sample
Input
1 1 1 1 1 1
Output
6Input
1 2 1 2 1 2
Output
13