CODY Contest 2020 CUP Challenge 全17题

第一题 Problem 1974. Length of a short side

Calculate the length of the short side, a, of a right-angled triangle with hypotenuse of length c, and other short side of length b.

给定直角(right-angled)三角形的斜边(hypotenuse)长c和一个短边长b,求另一个短边长a。直接用勾股定律:a=\sqrt{c^2-b^2}

function a = calculate_short_side(b, c)
  a = sqrt(c^2-b^2);
end

第二题 Problem 2018. Side of a rhombus

If a rhombus has diagonals of length x and x+1, then what is the length of its side, y?

如果菱形(rhombus)的两个对角线分别长为x和和x+1,求菱形的边长y。

由菱形的特征可知,两点对角线相互垂直,并且垂点为他们的中点,所以菱形可以看作四个边长相同的直角三角形,所以菱形的边长y可以看作直角三角形的斜边,并且两个直角边长度分别为对角线的一半。

function y = rhombus_side(x)
  y = sqrt((x/2)^2+((x+1)/2)^2);
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 2016. Area of an equilateral triangle

Calculate the area of an equilateral triangle of side x.

计算边长为x的等边三角形的面积,为第三题的反问题。

扫描二维码关注公众号,回复: 13016134 查看本文章
function y = equilateral_area(x)
  y = sqrt(3)/4*x*x;
end

第五题 Problem 2015. Length of the hypotenuse

Given short sides of lengths a and b, calculate the length c of the hypotenuse of the right-angled triangle.

给定直角三角形短边a和b,求斜边c。同样出现在题组 Introduction to MATLAB


function c = hypotenuse(a,b)
  c = sqrt(a^2+b^2);
end

第六题 和第一题重复出现

第七题 Problem 2024. Triangle sequence 

这道题算是这题组离比较有意思的之一,但是题目描述非常长:

A sequence of triangles is constructed in the following way:

1) the first triangle is Pythagoras' 3-4-5 triangle

2) the second triangle is a right-angle triangle whose second longest side is the hypotenuse of the first triangle, and whose shortest side is the same length as the second longest side of the first triangle

3) the third triangle is a right-angle triangle whose second longest side is the hypotenuse of the second triangle, and whose shortest side is the same length as the second longest side of the second triangle etc.

Each triangle in the sequence is constructed so that its second longest side is the hypotenuse of the previous triangle and its shortest side is the same length as the second longest side of the previous triangle.

What is the area of a square whose side is the hypotenuse of the nth triangle in the sequence?

大概就是说第一个三角形为边长为3.4.5的直角三角形,从第二个直角三角形开始,该三角形的第二长边(两短边的长边)等于上一个直角三角形的斜边,最短边为上一个直角三角形的第二长边。

因为不太好理解,就绘制了一个图

从左到右分别为第1.2.。。。n个三角形,第k个直角三角形的两个直角边是第k-1个直角三角形的最大两条边,将边长按照大小和出现顺序排列,那么为[3,4,5,sqrt(41),sqrt(66),...]第一个三角形由这个数组的第1到3个边构成,第二个为2到4,以此类推,有点像斐波那契数列,不过递进条件为根号下前两个数的平方和。

这道题要求为返回第n个三角形的斜边的平方,又因为边长的平方是直接满足斐波那契数列的,所以直接计算边长的平方即可:

function area = triangle_sequence(n)
  a(1)=9;
  a(2)=16;
  for i=1:n
      a(i+2)=a(i+1)+a(i);
  end
  area = a(n+2);
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

还有一种方法是对abc进行排序,然后第三个数就是斜边,前两个为短边,一样可以判断。

第九题 Problem 2022. Find a Pythagorean triple

Given four different positive numbers, a, b, c and d, provided in increasing order: a < b < c < d, find if any three of them comprise sides of a right-angled triangle. Return true if they do, otherwise return false .

给定四个数,abcd,满足a<b<c<d,判断是否有任意三个数可以组成直角三角形。

第一种方法就是判断四个条件满足一个:bcd/acd/abd/abc。第二种表达是把他们按照顺序放入数组,然后每次从种删掉一个,判断剩下的是否满足。

function flag = isTherePythagoreanTriple(a, b, c, d)
  
  for i=1:4
      abcd=[a,b,c,d];
      abcd(i)=[];
      if(abcd(1)^2+abcd(2)^2==abcd(3)^2)
          flag=true;
          return;
      end
  end
  flag= false;
end

第十题 Problem 2021. Is this triangle right-angled?

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

和第八题稍微不同,规定了c为最大边,直接判断即可:

function flag = isRightAngled(a, b, c)
  flag = c^2==a^2+b^2;
end

第十一题 Problem 2020. Area of an Isoceles Triangle

An isosceles triangle has equal sides of length x and a base of length y. Find the area, A, of the triangle.

给定等腰(isosceles)三角形的腰长x和底边y,求面积。

先求高,高与底边垂直于底边的中点,所以根据腰长和底边的一半计算得到高:

然后面积等于底乘以高除以2.

function A = isocelesArea(x,y)
  A = 0.5*y*sqrt(x^2-0.25*y^2);
end

第十二题 Problem 2019. Dimensions of a rectangle

The longer side of a rectangle is three times the length of the shorter side. If the length of the diagonal is x, find the width and length of the rectangle.

已知举行的长边是短边的三倍,并且对角线长为x,求长边(length)和短边(width)。

设宽(短边)为y,那么长为3y,根据勾股定理,10y^2=x^2,所以y=sqrt(0.1)x,长为3*y。

function [width, length] = findRectangleDimensions(x)
  width = sqrt(0.1)*x;
  length = 3*width;
end

第十三题 和第二题重复出现

第十四题 和第三题重复出现

第十五题 和第四题重复出现

第十六题 和第五题重复出现

第十七题 和第十一题重复出现

不知道这个题组里为啥有6个题重复出现两次,虽然有的题出现在其他题组里这个可以理解。

猜你喜欢

转载自blog.csdn.net/qq_36614557/article/details/110532417
CUP