Counting Triangles(hd1396)

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.

Counting Triangles(hd1396)


Input
The length n (n <= 500) of the equilateral 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.
 

 

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;
 1 #include <iostream>

 2 using namespace std;

 3 int main()

 4 {

 5     int n,i,j;

 6     long long a[501]={0};

 7     for(i=1;i<501;i++)

 8     {

 9         for(j=1;j<=i;j++)

10         {

11             a[i]+=(i-j+1+1)*(i-j+1)/2;

12             if(j*2<=i)

13                 a[i]+=(i-2*j+1+1)*(i-2*j+1)/2;

14         }

15     }

16     while(cin>>n)

17         cout<<a[n]<<endl;

18 }

 

 

你可能感兴趣的:(count)