uva 11401 数三角形
2013-07-23 19:55
169 查看
Triangle Counting
Input: Standard Input
Output: Standard Output
You are given n rods of length 1, 2…, n. You have to pick any 3 of them & build a triangle. How many distinct triangles can you make? Note that, two triangles will be considered different if they have at least 1 pair of arms with different length.
Input
The input for each case will have only a single positive integer n (3<=n<=1000000). The end of input will be indicated by a case with n<3. This case should not be processed.
Output
For each test case, print the number of distinct triangles you can make.
Sample Input Output for Sample Input
[align=center] [/align]
Input: Standard Input
Output: Standard Output
You are given n rods of length 1, 2…, n. You have to pick any 3 of them & build a triangle. How many distinct triangles can you make? Note that, two triangles will be considered different if they have at least 1 pair of arms with different length.
Input
The input for each case will have only a single positive integer n (3<=n<=1000000). The end of input will be indicated by a case with n<3. This case should not be processed.
Output
For each test case, print the number of distinct triangles you can make.
Sample Input Output for Sample Input
5 8 0 | 3 22 |
/* 设最大边长为x的三角形有c(x)个,跟三角形的定义两边之和大于第三边有x<y+z 变形下的x-y<z<x;当y=1时无解,当y=2时只有一个解z=x-1,知道y=x-1时又x-2个解 ,所以共有(x-1)(x-2)/2个解,由于题意中不能存在y=z的解所以y=z这部分解, 当x/2+1至x-1才存在y=z的可能,共有(x-1)/2个.还过有过程中每种三角形算了两遍 所以c(x)=((x-1)(x-2)/2-(x-1)/2)/2); f(n)=c(1)+c(2)+.....+c(n); */ #include<iostream> #include<cstdio> using namespace std; __int64 f[1000010]; void Init() { __int64 i;//用int定义结果Wrong answer,不定义__int64计算过程中会溢出 f[1]=0; f[2]=0; f[3]=0; for(i=4;i<=1000000;i++) f[i]=f[i-1]+((i-1)*(i-2)/2-(i-1)/2)/2; } int main() { Init(); int n; while(cin>>n,n>=3) printf("%I64d\n",f ); return 0; }
[align=center] [/align]
相关文章推荐
- Uva 11401 数三角形
- uva11401 - Triangle Counting(数三角形)
- UVa 11401 三角形的个数
- UVA 11401【数三角形】Triangle Counting------2015年1月24日
- UVa 11401 数三角形(组合数学)
- UVa 11401 Triangle Counting 数三角形
- 数学,计数(数三角形,uva 11401)
- uva 11401 Triangle Counting 数三角形 递推
- 数三角形Triangel Counting UVa 11401()
- uva 11401 Triangle Counting 数三角形 递推
- UVA 11401 TRIANGLE COUNTING 【加法原理】
- UVA 11186 - Circum Triangle(圆上三角形求法)
- Save the Students! UVALive - 5984 (几何判断点在三角形、圆、矩形)
- 【递推】【组合计数】UVA - 11401 - Triangle Counting
- UVA - 11401 - Triangle Counting(递推+找规律)
- UVA 10112 Myacm三角形
- UVA 11401 - Triangle Counting(数论+计数问题)
- UVA 438 求三角形外接圆的周长
- UVA 143 Orchard Trees(判断点在三角形内)
- uva 10250 The Other Two Trees(矩形三角形坐标计算)