您的位置:首页 > 其它

HDU 1396 Counting Triangles

2012-08-07 12:08 309 查看
[align=left]Problem Description[/align]
Given an equilateral triangle with n the length of its side, program to count how many triangles in it.


Input
The length n (n <= 500) of the equilateral triangle's side, one per line.

process to the end of the file

[align=left]Output[/align]
The number of triangles in the equilateral triangle, one per line.

Sample Input

1 2 3

Sample Output

1 5 13

做这个题目要了解,有关数三角形的公式,本质就是一题简单的数学题。

AC代码:
#include<iostream>
using namespace std;
int main()
{
__int64 a[501]={0,1};
int i,x,y;
for(i=2,x=y=0;i<=500;i++)
{
if(i%2==0){x++;}
else {y++;}
a[i]=a[i-1]+i*i-x*y;
}
int n;
while(scanf("%d",&n)!=EOF)
{printf("%I64d\n",a
);}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: