杭电1396

本文介绍了一个计算给定边长的等边三角形内包含的所有小三角形数量的问题。通过一个AC代码示例,展示了如何通过编程解决这个问题,并给出了具体的输入输出样例。

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

一道简单的找规律的题,昨天下午看的,晚上上物理课时想了想,本打算昨天晚上回寝室提交的,结果xd在寝室非要看什么微电影,,,我擦,,,结果一微微了125分钟。。。。。。。今天上午第一次提交,,,竟然wr,,,让我很是震惊,,,明明思路没错的啊。后来仔细想了想,我擦,,竟然还有倒着的三角形。。。。无语。下面是题:

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

下面是ac代码,,在机房上课,,网速不怎么给力,,代码不太好贴,,将就一下了。。。

#include <iostream>
using namespace std;
int main()
{
int n;
while(cin>>n)
{
int sum=0;
for(int i=1;i<n;++i)
sum+=i*(n-i);
sum+=n*n;
if(n>=4)
{
int k=n-3;
if(k==1)
{sum+=1;}
else
{
while(k>0)
{
for(int i=1;i<=k;++i)
sum+=i;
k-=2;
}
}
}
cout<<sum<<endl;
}
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值