matlabcody CUP Challenge

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?

这么大串英文看着挺复杂,其实就下个三角形的最小的两条边相当于上个三角形的最大两条边,然后勾股就能知道斜边…刚开始运行没注意看最后几句话,求的是以斜边为正方形的面积…

l1 = 5;l2 = 4;l3 = 3;
 for index = 1:n
     if (index ~= n)
    l3 = l2
    l2 = l1
     l1 = sqrt(l2^2+l3^2)
     end
 end
     
area = l1^2

然后刷评论区发现了这个链接
Calculating Fibonacci Numbers with Matrices and Linear Algebra

然后看了下别人的解法

大佬的写法就是这么简单且看不懂…
用了 regexp 匹配正则表达式函数
查了百度百科正则表达式,下次再(yi)学(ding)
百度百科

 regexp '' '(?@ area = (8+17/sqrt(5))*((1+sqrt(5))/2)^n + (8-17/sqrt(5))*((1-sqrt(5))/2)^n);';

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 = 5;b=12;c=13;
logic = (max([a,b,c])^2)-(min([a,b,c])^2)-(median([a,b,c])^2);
if logic == 0
    flag = true
else 
   flag = false
end

评论区看到的 用 sort 也不戳 简单多了

a = 5;b=12;c=13;
x=[a b c];
y=sort(x);
flag = y(1)^2+y(2)^2==y(3)^2

你可能感兴趣的:(matlab)