HDU/HDOJ 1396 浙大月赛2003年
2011-09-06 22:15
351 查看
Counting Triangles
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1160 Accepted Submission(s): 573
[align=left]Problem Description[/align]
Given an equilateral triangle with n the length of its side, program to count how many triangles in it.
[align=left]Input[/align]
The length n (n <= 500) of the equilateral triangle's side, one per line.
process to the end of the file
[align=left]Output[/align]
The number of triangles in the equilateral triangle, one per line.
[align=left]Sample Input[/align]
1 2 3
[align=left]Sample Output[/align]
1 5 13
[align=left]Author[/align]
JIANG, Jiefeng
[align=left]Source[/align]
ZOJ Monthly, June 2003
我是这样思考的。
可以看出来,边长+1意味着三角形多了一行
那么可以针对多的那一行做特殊计算,因为上一次边长的三角形是没有变化的!
经过非常穷凶极恶的公式推导终于得到了最后的答案
我的代码:
#include<stdio.h> int ans[505]; int cal(int n) { int t1,t2,t3; t1=2*n-1; t2=(n-1)*n/2; if(n&1) t3=(n-1)*(n-3)/4; else t3=(n-2)*(n-2)/4; return t1+t2+t3; } void init() { int i; ans[1]=1; for(i=2;i<=500;i++) ans[i]=ans[i-1]+cal(i); } int main() { int n; init(); while(scanf("%d",&n)!=EOF) { printf("%d\n",ans ); } return 0; }
相关文章推荐
- HDU/HDOJ 1395 ACM浙大月赛 2^x mod n = 1
- HDU/HDOJ 1395 ACM浙大月赛 2^x mod n = 1 可暴力 可用欧拉定理
- HDU/HDOJ 3400 Line belt 三分嵌套 杭电月赛
- HDU/HDOJ 1004 Let the Balloon Rise(颜色统计)
- HDU/HDOJ 1084 What Is Your Grade?
- HDU/HDOJ 1242 Rescue 典型的迷宫广度优先搜索题
- HDU/HDOJ1851 nim博弈和巴什博弈的结合
- hdu/hdoj 1075 What Are You Talking About
- hdu/hdoj 1151 Air Raid
- HDU/HDOJ 3902 Swordsman 多校联合7
- HDU/HDOJ 4002 2011大连赛区网络赛 数论
- HDU/HDOJ 3215 The first place of 2^n
- 畅通工程续(2008浙大研究生复试热身赛[最短路径] hdoj 1874)
- HDU/HDOJ 1548 A strange lift BFS,DFS
- HDU/HDOJ 2563 统计问题 回溯发DFS
- hdu/hdoj 1232 畅通工程---并查集
- ZOJ 3723 (浙大月赛)状压DP
- HDU/HDOJ 2255 奔小康赚大钱 HDOJ 2008 Summer Exercise(4)- Buffet Dinner
- HDU/HDOJ 3666 差分约束 2010 Asia Regional Harbin
- HDU/HDOJ 4004 2011大连赛区网络赛 D题