Counting Triangles
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2506 Accepted Submission(s):
1184
Problem Description
Given an equilateral triangle with n the length of its
side, program to count how many triangles in it.
side, program to count how many triangles in it.
Input
The length n (n <= 500) of the equilateral
triangle's side, one per line.
triangle's side, one per line.
process to the end of the file
Output
The number of triangles in the equilateral triangle,
one per line.
one per line.
Sample Input
1
2
3
Sample Output
1
5
13
分正三角形和倒三角形,
当长度为n时,在最低边上长度为x的正三角形,个数为n-x+1,所以总的个数为(n-x+1+1)*(n-x+1)/2
在最低边上长度为x的倒三角形,个数为n-x*2+1(2*x<=n),所以总的个数为(n-2*x+1+1)*(n-2*x+1)/2;
#include <iostream>
using namespace std;
int main()
{
int n,i,j;
long long a[]={};
for(i=;i<;i++)
{
for(j=;j<=i;j++)
{
a[i]+=(i-j++)*(i-j+)/;
if(j*<=i)
a[i]+=(i-*j++)*(i-*j+)/;
}
}
while(cin>>n)
cout<<a[n]<<endl;
}