CODY Contest 2020 Basics - Triangles 全9题

第一题 Problem 769. Calculate the area of a triangle between three points

Calculate the area of a triangle between three points:

P1(X1,Y1)

P2(X2,Y2)

P3(X3,Y3)

these three points are the vertices of the triangle.

给定三角形的三个点的坐标,求面积。

首先求出P1~P3三个点的距离,即三角形边长,存放在d向量中,进而求得三角形的半周长p为d的和的一半,即p=sum(d)/2;然后根据海伦公式,有s=sqrt(p*(p-a)*(p-b)*(p-c)),abc分别对应d(1)-d(3),所以:

% X=[x1 x2 x3]
% Y=[y1 y2 y3]
function y = your_fcn_name(X,Y)
  for i=1:3
      d(i)=sqrt((X(i)-X(mod(i,3)+1))^2+(Y(i)-Y(mod(i,3)+1))^2);
  end
  p=0.5*sum(d);
  y=sqrt(p*(p-d(1))*(p-d(2))*(p-d(3)));
end

第二题 Problem 2017. Side of an equilateral triangle

If an equilateral triangle has area A, then what is the length of each of its sides, x?

等边(equilateral)三角形的面积为A,求边长x。根据等边三角形的面积公式有,,所以

 
function x = side_length(A)

    x = sqrt(4*A/sqrt(3));

end

第三题 Problem 2023. Is this triangle right-angled?

Given any three positive numbers a, b, c, return true if the triangle with sides a, b and c is right-angled. Otherwise, return false.

给定任意边长a,b,c判断组成的三角形是否为直角三角形,注意,c不是斜边。

所以需要找出a b c中的最大边作为斜边,其他两个短边作为直角边,然后运用勾股定理判断。

function flag = isRightAngled(a,b,c)

    abc=[a,b,c];

    max_abc=max(abc);

    flag =(max_abc)^2==sum(abc(abc<max_abc).^2);

end

第四题 Problem 43236. Find my daddy long leg (No 's')

Given the ratio of the two legs (longer / shorter), and the hypotenuse length, find the value of the bigger leg.

给定两个短边的比例(大于1),以及斜边(最长边)的长度,求短边的较大边。题目中没说直角三角形,但是斜边(hypotenuse )是直角三角形的概念,所以根据勾股定理,已知斜边x和直角边比例ratio,设最短边为y,那么y^2+ratio^2y^2=x^2;

y=x/sqrt(1+ratio^2);较长边为ratio*y.

function y = myDaddyLongLeg(x,ratio)
  y = ratio*x/sqrt(1+ratio^2);
end

第五题 Problem 1103. Right Triangle Side Lengths (Inspired by Project Euler Problem 39)

If p is the perimeter of a right angle triangle with integral length sides, { a, b, c }, there are exactly three solutions for p = 120.

{[20,48,52], [24,45,51], [30,40,50]}

Given any value of p, return a cell array whose elements are the sorted side lengths of a possible right triangle whose perimeter is p. Furthermore, the elements of the output should be sorted by their shortest side length.

如果p是直角三角形的周长,那么存在三个直角三角形满足周长为p。给定p,返回一个1:n的cell,表示共n个直角三角形的周长为p,并且cell每个元素为一个向量,保存三个边长,并按从小到大排列。注意,边长为整数。

这道题源自于欧拉project的第9题,就是把原题的输出三个边的乘积改为了把所有满足的三角形边长保存下来。之前在欧拉十题的第九题做法是算出了短边中的较长边b的范围,这道题因为需要对最短边进行排序,所以改为了计算a的范围,和b相似,a的范围是1:floor(p/(sqrt(2)+2)),然后遍历a,b可以表示为(p^2-2*a*p)/(2*p-2*a),判断b是否为整数,如果为整数,那么保存[a,b,c],由于cell用的少,我先把所有边长存在了一个n*3的矩阵d中,然后把矩阵的每一行作为c的一个元素。

function c = right_triangle_sides(p)
    k=1;
    for a=1:floor(p/(sqrt(2)+2))
        b=(p^2-2*a*p)/(2*p-2*a);
        if mod(b,1)==0
            d(k,1:3)=[a,b,sqrt(a^2+b^2)];
            k=k+1;
        end
    end
    c=cell(1,k-1);
    for i=1:k-1
        c{i}=d(i,:);
    end
end

第六题 Problem 558. Is the Point in a Triangle?

Check whether a point or multiple points is/are in a triangle with three corners

Points = [x, y];

Triangle = [x1, y1; x2, y2; x3, y3]

Return true or false for each point tested.

For example,

input: Points = [0, 0.5]; Triangle = [0, 0; 1, 0; 1, 1]

output: y = 0;

还没看题意我就知道要干啥了,给定三角形的三个点坐标,判断给定点坐标是否在三角形内(题意是in,就只按in做,不考虑在边上)

说实话,数学快忘干净了,如果p在三角形ABC内,那么P到A B C三点的夹角和为180度。就记得这个了。

对于每个点P,给定的三个点ABC的坐边减去P的坐标就得到了PA PB PC三个向量,然后求三个向量两两的夹角:theta=acos(dot(a,b)/norm(a,2)/norm(b,a))。a和b分别为两个向量。然后判断夹角和是否为2pi,由于取整、截尾存在误差,判断相等时要给定一个误差范围。

function y = your_fcn_name(Points, Triangle)
    y=zeros(1,size(Points,1));
    for i=1:size(Points,1)
        p=Triangle-Points(i,:);
        for j=1:3
            theta(j)=acos(dot(p(j,:),p(mod(j,3)+1,:))/norm(p(j,:),2)/norm(p(mod(j,3)+1,:),2));
        end
        if abs(sum(theta)-2*pi)<=1e-5
            y(i)=1;
        end
    end
end

p就表示了P点出发到三个点的向量。theta储存三个向量的两两夹角。

第七题  Problem 42855. Height of a right-angled triangle

Given numbers a, b and c, find the height of the right angled triangle with sides a and b and hypotenuse c, for the base c. If a right angled triangle with sides a and b and hypotenuse c does not exist, return NaN (not-a-number).

给定abc三个边,求a、b、c构成的直角三角形并且以c为斜边,求c为底的高度,如果不存在此直角三角形,返回Nan。

直角三角形的面积S=1/2*a*b=1/2*c*h,h为c底的高,所以h=a*b/c,所以先根据勾股定理判断是否为直角三角形,然后计算h即可。

好家伙测试样例不讲武德,竟然abc为0或者为负。逐个判断有点麻烦,直接加一个为nan的条件:sum([a,b,c]<=0)>0即可,即判断abc小于等于0的数量是否大于0.

function h = triangle_height(a, b, c)
  if a^2+b^2~=c^2 || sum([a,b,c]<=0)>0
      h=nan;
  else
      h=a*b/c;
end

第八题 Problem 43599. Find the sides of an isosceles triangle when given its area and height from its base to apex

Find the sides of an isosceles triangle when given its area and the height from its base to apex.

For example, with A=12 and h=4, the result will be [5 5 6].

给定等腰三角形的面积和底的高,返回三个边。

测试样例是判断输出和实际值的绝对差的sum,所以对顺序有要求的,测试样例的顺序是前两个是腰长,第三个为底边长。

根据面积和高能求得底边,再以底边的一半和高做勾股定理可到的腰。

function y = sidesOfTheTriangle(A,h)
  c=A*2/h;
  a=sqrt(0.25*c^2+h^2);
  y=[a a c];
end

第九题 Problem 43294. Can we make a triangle?

Given three positive number, check whether a triangle can be made with these sides length or not. remember that in a triangle sum of two sides should be greater than the third one. So with the lengths of 2,3 and 6 we can not make a triangle

给三个正数,判断以其为边长是否能组成三角形。对三个数进行排序,判断两个小的和是否大于最大的数。

其实只需要找到最大的就可以:所以我判断a和b中的最大值是否大于c,如果大于c,c就是a和b的最大值,然后进而判断a和b谁大,谁大谁就替换为c修改前的值,也就是说c和最大值的数进行了互换。

然后判断a+b>c即可。

function flag = Is_Triangle(a, b, c)
  if max([a,b])>c
      tmp=c;
      c=max([a,b]);
      if a>b
          a=tmp;
      else 
          b=tmp;
      end
  end
      flag=a+b>c;
end

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值