Peck Chen

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

/*******************************************************************************
递推:a[n] = a[n-1] + n * (n + 1) / 2; 但这并不是结果。

还有一种情况是最底下一行顶角朝下的三角形还没有计算

没有考虑这样情况,我也错了...hehe
观察规律得:left = (n-1) + (n-1)-2 + (n-1)-4... + last(n为奇数, last为2, 反之为1)
所以a[n] = a[n] + left;

*******************************************************************************/

 

2010-11-22 23:08:04 Accepted 1396 0MS 204K 306 B C Y

 

 

代码
#include <stdio.h>
#define N 501

int main()
{
int i, n, num[N];;

num[
1] = 1;
for (i = 2; i < N; i++)
{
num[i]
= num[i-1] + i * (i + 1) / 2; /* 第一种情况 */
n
= i - 1;
while (n > 0) /* 第二种情况 */
{
num[i]
+= n;
n
-= 2;
}
}

while (scanf("%d", &n) != EOF)
{
printf(
"%d\n", num[n]);
}
}

 

 

 

 

 

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

posted on 2010-11-22 23:10  PeckChen  阅读(972)  评论(0编辑  收藏  举报