Problem Description
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
process to the end of the file
Output
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[n]);}
return 0;
}