24h購物| | PChome| 登入
2012-06-21 15:46:14| 人氣1,454| 回應0 | 上一篇 | 下一篇

[UVA] 11579 - Triangle Trouble

推薦 0 收藏 0 轉貼0 訂閱站台

Problem C: Triangle Trouble

These are also limited edition triangles

There is trouble at the triangle factory. The triangle assembler has gone down, so all that has been produced for the day is a bunch of triangle sides. To make the best of this situation, it has been decided to create the triangle with the largest possible area from the available sides, and sell it as a limited edition triangle.

You have been hired to write a program that will determine the area of the limited edition triangle.

Input begins with the number of test cases on its own line. Each test case begins with a positive integer N (3 ≤ N ≤ 10,000), followed by N positive real numbers si representing the lengths of the available triangle sides (0 < si ≤ 100,000). A single test case may be spread out over several consecutive lines of the input.

For each test case, output a line containing the largest possible area of a triangle built using three of the given sides (as a real number rounded to 2 decimal places). If it is not possible to construct any triangles then output "0.00" (quotes for clarity).

Sample input

2
4 3.0 4.0 5.0 100.0
3 1.0 2.0 4.0

Sample output

6.00
0.00


重新寫過了, 優化了許多部份,
答案的部分仍然是將邊做排序, 找連續三個可以湊成三角形的邊構成的最大三角形

#include <stdio.h>
#include <math.h>
#include <algorithm>
using namespace std;
inline double ReadPoint() {
static char c;
int p = 0;
double t = 1;
while((c = getchar()) >= '0')
t /= 10, p = (p << 3) + (p << 1) + (c-'0');
return (double)p*t;
}
inline int ReadDouble(double *x) {
static char c, neg;
while((c = getchar()) < '-') {if(c == EOF) return EOF;}
if(c == '.') {*x = ReadPoint();return 0;}
neg = (c == '-') ? -1 : 1;
*x = (neg == 1) ? c-'0' : 0;
while((c = getchar()) >= '0')
*x = (*x)*10 + c-'0';
if(c == '.') *x += ReadPoint();
*x *= neg;
return 0;
}
int main() {
int t, n, i;
double A[10000];
scanf("%d", &t);
while(t--) {
scanf("%d", &n);
for(i = 0; i < n; i++) {
ReadDouble(A+i);
}
sort(A, A+n);
double area = 0.0, tmp, s;
for(i = 2; i < n; i++) {
if(A[i-2]+A[i-1] <= A[i])
continue;
s = (A[i-2]+A[i-1]+A[i])/2;
tmp = s*(s-A[i-2])*(s-A[i-1])*(s-A[i]);
if(area < tmp)
area = tmp;
}
area = sqrt(area);
printf("%.2lf\n", area);
}
return 0;
}

台長: Morris
人氣(1,454) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 784 - Maze Exploration
此分類上一篇:[UVA][Bitset技巧] 11466 - Largest Prime Divisor

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文