7144. Different Triangles
Constraints
Time Limit: 1 secs, Memory Limit: 256 MB
Description
Given N sticks of different length, please find out how many different ways you can form a triangle by three of these sticks.
Input
The first line contains an integer T (T<=20), indicating the number of test cases. Each case contains two lines. The first line contains an integer N (3<=N<=100). The second line contains N different positive integers not greater than 1000, indicating the lengths of sticks.
Output
For each case, output the number of ways to form a triangle.
Sample Input
231 2 342 3 4 5
Sample Output
03
Hint
In the second sample, you can form a triangle using (2, 3, 4), (2, 4, 5) or (3, 4, 5).
Problem Source
“星海通杯”第四届中山大学ICPC新手赛 by 林瀚
#include <iostream>
using namespace std;
int main () {
int T;
cin>>T;
while (T--) {
int N;
cin>>N;
int a[101];
for (int i = 0; i < N; i++) {
cin>>a[i];
}
int p;
int q;
int r;
int count = 0;
bool flag = true;
for (p = 0; p < N - 2; p++) {
for (q = p + 1; q < N - 1; q++) {
for (r = q + 1; r < N; r++) {
if (a[p] + a[q] <= a[r] || a[p] + a[r] <= a[q] || a[r] + a[q] <= a[p]) {
flag = false;
}
if (flag == true)
count++;
flag = true;
}
}
}
cout<<count<<endl;
}
//system("pause");
return 0;
}