杭电1396
一道简单的找规律的题,昨天下午看的,晚上上物理课时想了想,本打算昨天晚上回寝室提交的,结果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
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;
}