UVA11437 - Triangle Fun

就是给一个三角形,然后给出一些关系,以及一些点的坐标,最后求一个三角形面积就好了。

我的做法:

用向量的方法推出离线段一个端点最近的三等分点的表达式

用行列式解线性方程推出求两直线交点的表达式

用叉积求三角形面积

用小数点后第一位对整数部分进行四舍五入求得离这个实数最近的整数

我的代码如下:

#include<iostream>
#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
struct Dot
{
	double x,y;
	Dot(){}
	Dot(double a,double b){x=a,y=b;}
};
Dot operator - (Dot a,Dot b){return Dot(b.x-a.x,b.y-a.y);}
double operator * (Dot a,Dot b){return a.x*b.y-b.x*a.y;}
double area(Dot a,Dot b,Dot c){return fabs((b-a)*(c-a));}
Dot cross(Dot a,Dot b,Dot c,Dot d)
{
	double e,f,g,h,i,j,k,l,m;
	e=b.y-a.y;f=a.x-b.x;g=a.x*b.y-a.y*b.x;
	h=d.y-c.y;i=c.x-d.x;j=c.x*d.y-c.y*d.x;
	k=Dot(e,h)*Dot(f,i);
	l=Dot(g,j)*Dot(f,i);
	m=Dot(e,h)*Dot(g,j);
	return Dot(l/k,m/k);
}
Dot d3(Dot a,Dot b){return Dot((2*a.x+b.x)/3,(2*a.y+b.y)/3);}
int main()
{
	int n,ans;
	Dot a,b,c,d,e,f,p,q,r;
	cin>>n;
	while(n--)
	{
		cin>>a.x>>a.y>>b.x>>b.y>>c.x>>c.y;
		d=d3(b,c);
		e=d3(c,a);
		f=d3(a,b);
		p=cross(b,e,d,a);
		q=cross(b,e,c,f);
		r=cross(d,a,c,f);
		ans=0.5+area(p,q,r)/2;
		printf("%d\n",ans);
	}
}
时间: 2024-11-08 07:03:36

UVA11437 - Triangle Fun的相关文章

(leetcode题解)Pascal&#39;s Triangle

Pascal's Triangle  Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 题意实现一个杨辉三角. 这道题只要注意了边界条件应该很好实现出来,C++实现如下 vector<vector<int>> generate(int

Lab 1: Write a java program for the triangle problem and test the program with Junit.

Tasks: 1. Install Junit(4.12), Hamcrest(1.3) with Eclipse 将两个jar包添加到工程中 2. Install Eclemma with Eclipse 3. Write a java program for the triangle problem and test the program with Junit. [Description of triangle problem]Function triangle takes three i

Solution to Triangle by Codility

question: https://codility.com/programmers/lessons/4 we need two parts to prove our solution. on one hand, there is no false triangular. Given the array has been sorted, if A[i]+A[i+1]>A[i+2], we can prove the existence of the triangle. for array A i

LeetCode (13) Pascal&#39;s Triangle (杨辉三角 )

题目描述 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return 从第三行开始,每行除了最左边和最右边两个数为1,其他数字都是上一行中相邻两个数字之和.根据上述规则可以写出下面的代码: class Solution { public: vector<vector<int> > generateRow1() { vector<in

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

POJ 1163 The Triangle

题目链接:http://poj.org/problem?id=1163 The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39022   Accepted: 23430 Description 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 (Figure 1) Figure 1 shows a number triangle. Write a program that calculat

LeetCode--Pascal&#39;s Triangle

Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] class Solution { public: vector<vector<int> > generate(int numRows) { vector<vector<in

? (triangle)

2.1 题目描述 给定一个无自环重边的无向图,求这个图的三元环1的个数以及补图2的三元环个数. 2.2 输入格式 第一行 2 个数 n, m ,分别表示图的点数.边数. 接下来 m 行,每行两个数 u, v ,表示一条连接 u, v 的无向边. 2.3 输出格式 一行两个数,依次表示原图的三元环个数以及补图的三元环的个数. 2.4 样例输入 5 5 1 2 1 3 2 3 2 4 3 4 2.5样例输出 2 1 2.6数据范围 对于 30% 的数据:n ≤ 100 对于 60% 的数据:m ≤

POJ 2079 Triangle [旋转卡壳]

Triangle Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 9525   Accepted: 2845 Description Given n distinct points on a plane, your task is to find the triangle that have the maximum area, whose vertices are from the given points. Input