三角形

 
 

Input

题目有多组数据,对于每组数据,第一行为一个整数n(3<=n<=100)代表棍子的数量,接下来一行为n个棍子的长度,长度的范围为1到100000.

Output

对于每组数据输出方案数。

Sample Input

4
1 2 3 4
3
1 2 3

Sample Output

1
0

 
 
#include<iostream>
using namespace std;
void QuickSort(int e[], int first, int end)
{
	int i = first, j = end;
	int temp = e[first];//记录第一个数据  

	while (i<j)
	{
		while (i<j && e[j] >= temp)  //与first数据比较,右边下标逐渐左移  
			j--;

		e[i] = e[j];

		while (i<j && e[i] <= temp)  //与first数据比较,左边下标逐渐右移  
			i++;

		e[j] = e[i];
	}
	e[i] = temp;                      //将first数据放置于i=j处  

	if (first<i - 1)
		QuickSort(e, first, i - 1);
	if (end>i + 1)
		QuickSort(e, i + 1, end);
}

void main()
{
	int n;
	while (cin>>n)
	{
		int count=0;
		int *len = new int[n];
		for (int i = 0; i < n; i++) cin >> len[i];
		QuickSort(len, 0, n-1);
		for (int a = 0; a < n; a++)
		{
			for (int b = a+1; b < n; b++)
			{
				for (int c = b+1; c < n; c++)
				{
					if (len[a]+len[b]>len[c])
						count++;
					else break;
				}
			}
		}
		cout << count << endl;
	}
}

你可能感兴趣的:(三角形)