ZOJ 1629 Counting Triangles(数学题 递推)

本程序解决了一个简单的动态规划问题:给定等边三角形的边长n,计算该三角形内部包含的所有小三角形的数量。输入为等边三角形的边长,输出则是对应的三角形数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值