欢迎访问我的个人网站==》 jiashubing.cn

ZOJ 1629 Counting Triangles(数学题 递推)

Counting Triangles

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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


Output

The number of triangles in the equilateral triangle, one per line.


Sample Input

1
2
3


Sample Output

1
5
13

 

如果说所有的递推都是动态规划,那这是最简单的动态规划了

View Code
 1 # include<stdio.h>
 2 int n;
 3 int f[501];
 4 void init(){
 5     int i,j;
 6     f[0]=0;
 7     for(i=1;i<501;i++)
 8         f[i]=f[i-1]+i*(i/2)+i*(i+1)/2-(i/2)*(i/2);
 9 }
10 int main(){
11     init();
12     while(scanf("%d",&n)!=EOF)
13         printf("%d\n",f[n]);
14     return 0;
15 }

 

posted @ 2013-04-08 17:28  贾树丙  阅读(403)  评论(0编辑  收藏  举报