HDU 1396 Counting Triangles

  数学基础第一弹!

  简单的递归题,没有什么可说的。只是要细心细心再细心,不要心浮气躁,急于求成。

  

 1 #include <iostream>
 2 #include <cstdio>
 3 
 4 using namespace std;
 5 
 6 long long int CountTriangle[1000010];
 7 
 8 int main()
 9 {
10     int i,j,n;
11 
12     long long int sum;
13 
14     CountTriangle[1] = 1;
15     CountTriangle[0] = 0;
16 
17     for(i = 2;i <= 500; ++i)
18     {
19         for(n = i/2,sum = 2*i-1,j = 2;j <= n; ++j)
20         {
21             sum += i-j+1 + i-j-j+1;
22         }
23         for(;j <= i; j++)
24             sum += i-j+1;
25         CountTriangle[i] = sum + CountTriangle[i-1];
26     }
27 
28     while(cin>>n)
29     {
30         if(n <= 0)
31             cout<<0<<endl;
32         else
33             cout<<CountTriangle[n]<<endl;
34     }
35 
36     return 0;
37 }
View Code

 

你可能感兴趣的:(HDU 1396 Counting Triangles)