?? 1396 counting triangles.cpp
字號:
/*
1396 Counting Triangles
Time Limit : 1000 ms Memory Limit : 32768 K Output Limit : 256 K
GUN C++
*/
/*
邊長為1的三角個數為n*n
總邊長n(n>=2)中邊長為m(m>=2)的正三角的個數為[ n-(m-1) ][ n-(m-2) ]/2個
倒三角可以看作是正三角的倒像
注意倒三角是增加2層才會出現邊長增加的新倒三家情況
但是已經出現的倒三角形狀是隨層數增加而增加
總邊長n(n>=4)中邊長為m(m>=2)的倒三角的個數為[ n-(m-1) ][ n-(m-2) ]/2個
*/
#include <iostream.h>
using namespace std;
const int Max=1000;
int main()
{
int n,ca,ans,rn;
while(cin>>n)
{
ans=n*n;//邊長為1的三角個數
for(ca=2;ca<=n;ca++)//邊長為ca(ca>=2)的正三角的個數
{
ans+=(n-ca+1)*(n-ca+2)/2;
}
for(ca=4;ca<=n;ca+=2)//總邊長為ca(ca>=2)的倒三角的個數
{
ans+=(n-ca+1)*(n-ca+2)/2;
}
cout<<ans<<endl;
}
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -