uva 194 - Triangle(几何)

题目链接:uva 194 - Triangle

注意两边一角,并接角的对边确定时(即用正弦定理求解时,可能会有多解的情况)

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;
const double pi = 4 * atan(1);
const double eps = 1e-4;

double A[6];

void CornerToCorner () {
	double t = 0;
	for (int i = 1; i < 6; i += 2) {
		if (A[i] < 0) continue;
		t += A[i];
	}
	for (int i = 1; i < 6; i += 2)
		if (A[i] < 0) A[i] = pi - t;
}
double getAngle(double a, double b, double c) {
	return acos( (a * a + b * b - c * c) / (2 * a * b) );
}

void EdgeToCorner () {
	for (int i = 1; i < 6; i += 2) {
		if (A[i] < 0)
			A[i] = getAngle( A[(i+1)%6], A[(i+3)%6], A[i-1]);
	}
}

double getEdge(double x, double y, double c) {
	//printf("%lf %lf\n", c, cos(c));
	return sqrt(x * x + y * y - 2 * x * y * cos(c));
}

bool judge () {
	for (int i = 0; i < 6; i++)
		if (A[i] < 0)
			return false;
	if (fabs(A[1] + A[3] + A[5] - pi) > eps) {
		return false;
	}

	if (A[0] + A[2] <= A[4] || A[2] + A[4] <= A[0] || A[0] + A[4] <= A[2]) {
		return false;
	}

	/*
	for (int i = 0; i < 6; i += 2)
		printf("%lf\n", A[i] / sin(A[i+1]));
		*/

	for (int i = 0; i < 4; i += 2) {
		//if (fabs(A[i] / sin(A[i+1]) - A[i+2] / sin(A[i+3])) > eps)
		//printf("%lf\n", fabs(A[i] * sin(A[i+3]) - sin(A[i+1]) * A[i+2]));
		if (fabs(A[i] * sin(A[i+3]) - sin(A[i+1]) * A[i+2]) > eps)
			return false;
	}
	return true;
}

int main () {
	int cas;
	scanf("%d", &cas);
	while (cas--) {
		int S = 0, C = 0, E = 0;
		bool flag = false;
		for (int i = 0; i < 6; i++) {
			scanf("%lf", &A[i]);
			if (A[i] > 0) {
				S++;
				if (i&1) C++;
				else E++;
			}
		}

		if (S < 3) {
			printf("Invalid input.\n");
			continue;
		}

		while (S < 6) {
			int T = S;
			if (C == 2) {
				CornerToCorner();
				S++, C = 3;
			}

			if (E == 3) {
				EdgeToCorner();
				S += 3 - C, C = 3;
			}

			for (int i = 0; i < 6; i += 2) {
				if (A[i] > 0 && A[i+1] > 0) {
					for (int j = 0; j < 6; j += 2) {
						if ((A[j] > 0 && A[j+1] > 0) || (A[j] < 0 && A[j+1] < 0)) continue;

						if (A[j] < 0) {
							A[j] = A[i] / sin(A[i+1]) * sin(A[j+1]);
							//printf("%lf %lf!!!!!\n", A[i] / sin(A[i+1]), A[j] / sin(A[j+1]));
							S++, E++;
						} else {
							if (A[i+1] < pi / 2 && A[i] < A[j] && A[i] > A[j] * sin(A[i+1]))
								flag = true;
							double tmp = sin(A[i+1]) * A[j] / A[i];
							if (tmp > 1 || tmp < 0)
								continue;
							A[j+1] = asin(sin(A[i+1]) * A[j] / A[i]);
							S++, C++;
						}
					}
				}
			}

			if (E == 2) {
				for (int i = 0; i < 6; i += 2) {
					if (A[i] < 0 && A[i+1] > 0) {
						A[i] = getEdge(A[(i+2)%6], A[(i+4)%6], A[i + 1]);
						S++, E++;
					}
				}
			}
			if (T == S)
				break;
		}

		//printf("%d\n", S);
		/*
		for (int i = 0; i < 6; i++)
			printf("%lf%c", A[i], i == 5 ? '\n' : ' ');
			*/

		if (judge()) {
			if (flag)
				printf("More than one solution.\n");
			else {
				for (int i = 0; i < 6; i++)
					printf("%lf%c", A[i], i == 5 ? '\n' : ' ');
			}
		} else
			printf("Invalid input.\n");

	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-25 00:21:56

uva 194 - Triangle(几何)的相关文章

UVA - 11437 - Triangle Fun (计算几何~)

UVA - 11437 Triangle Fun Time Limit: 1000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description Problem A Triangle Fun Input: Standard Input Output: Standard Output In the picture below you can see a triangle ABC. Point D, E

UVA 11401 - Triangle Counting(数论+计数问题)

题目链接:11401 - Triangle Counting 题意:有1,2,3....n的边,求最多能组成的三角形个数. 思路:利用三角形不等式,设最大边为x,那么y + z > x 得 x - y < z < x 然后y取取值,可以从1取到x - 1,y为n时候,有n - 1个解,那么总和为0 + 1 + 2 +...+ (x - 2) = (x - 1) * ( x- 2) / 2; 然后扣除掉重复的y = z的情况,在y > x / 2时,每个y取值会出现一次y = z.

UVA - 11401 - Triangle Counting (递推!)

UVA - 11401 Triangle Counting Time Limit: 1000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description Problem G Triangle Counting Input: Standard Input Output: Standard Output You are given n rods of length 1, 2-, n. You have

uva 11401 - Triangle Counting(数论)

题目链接:uva 11401 - Triangle Counting 题目大意:有多少种方法可以从1,2,3...n中选出3个不同的数组成三角形,给出n,求种数. 解题思路:加法原理,设最大边为x的三角形有c(x)个,那么另外两条边长分别为y和z,根据三角形的形式可以的y+z>x,所以z的范围即为x?y<z<x 根据这个不等式可以得到每个y值所对应的z值个数,为等差数列,所以 c(x)=(x?1)?(x?2)2??x?12?2 然后根据递推:f(n)=∑i=1nc(i) 代码 #incl

UVA - 11186 Circum Triangle (几何)

题意:有N个点,分布于一个圆心在原点的圆的边缘上,问所形成的所有三角形面积之和. 分析: 1.sin的内部实现是泰勒展开式,复杂度较高,所以需预处理. 2.求出每两点的距离以及该边所在弧所对应的圆周角.一条弧所对圆周角等于它所对圆心角的一半. 3.S = 1/2*absinC求三角形面积即可. #include<cstdio> #include<cstring> #include<cstdlib> #include<cctype> #include<c

UVa 488 - Triangle Wave

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=94&page=show_problem&problem=429 题目:每个例子输入2个数,一个是wave的幅度,一个是重复的个数. 例如:Input1 32 output:122333221 122333221 思路: 将1,22,333,···,999999999.字符串存在数组里.按照幅度打印输出就好.然后注意一

UVA 11401 - Triangle CountingTriangle Counting 数学

You are given n rods of length 1,2, . . . , n. You have to pick any 3 of them and build a triangle. Howmany distinct triangles can you make? Note that, two triangles will be considered di?erent if they haveat least 1 pair of arms with di?erent length

UVA 11346 Probability (几何概型, 积分)

题目链接:option=com_onlinejudge&Itemid=8&page=show_problem&problem=2321">https://uva.onlinejudge.org/index.php? option=com_onlinejudge&Itemid=8&page=show_problem&problem=2321 题目大意:在A是一个点集 A = {(x, y) | x ∈[-a, a],y∈[-b, b]},求取出

UVA LIVE-4413 - Triangle Hazard

给个图,告诉R,P,Q三点的坐标,求出A,B,C三点的坐标 我的做法: 根据梅涅劳斯定理列出三个二元一次方程组,求出pb,qc,ra的长度,然后用点位移求出A,B,C三点的坐标即可 我的代码: #include<iostream> #include<map> #include<string> #include<cstring> #include<cstdio> #include<cstdlib> #include<cmath&g