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

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 .

给四个从小到大的数,判断里面能不能组成一个直角三角形
本来想着,敲了这么久代码不能写出那么多 if 的 low 代码
结果…

%Size: 89 Leading solution size is 8.
a = 5;b=12;c=13;d = 15;
num1 = [a,b,c];
num2 = [c,d];
count = 0;
index = 0;
for i = num1
    for j = num2
         if ismember(sqrt(j^2-i^2),num1)
            count =count + 1;
            index = index + 1;
            break
         end
         if index ==  1
             break
         end
    end
end
    if count~=0
    flag = true
    else
    flag = false
    end

别人写的两行搞定…

x = [a b c d].^2
flag = any(ismember(x,x'+x));

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.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Sink Arsenic

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值