ZOJ 1629 Counting Triangles(数学题 递推)
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 }
把每一件简单的事情做好,就是不简单;把每一件平凡的事情做好,就是不平凡!相信自己,创造奇迹~~