Matlab Cody——CUP Challenge 题解

1. Problem 1974. Length of a short side

1
2
3
function a = calculate_short_side(b, c)
a = sqrt(c.^2-b.^2)
end

2. Problem 2024. Triangle sequence

1
2
3
4
5
6
function area = triangle_sequence(n)
a = [0 1;1 1]^n;
x = [9;16];
res = a*x;
area = res(2);
end

3. Problem 2022. Find a Pythagorean triple

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function flag = isTherePythagoreanTriple(a, b, c, d)
tol = 1e-5;
if abs(a^2+b^2 - c^2) <= tol
flag = true;
elseif abs(a^2+b^2 - d^2) <= tol
flag = true;
elseif abs(a^2+c^2 - d^2) <= tol
flag = true;
elseif abs(b^2+c^2 - d^2) <= tol
flag = true;
else
flag = false;
end
end

4. Problem 2023. Is this triangle right-angled?

1
2
3
4
5
6
7
8
9
function flag = isRightAngled(a,b,c)
temp = [a,b,c];
temp = sort(temp,2,'ascend');
tol = 1e-5;
if abs(temp(1)^2+temp(2)^2-temp(3)^2)<=tol
flag = true;
else
flag = false;
end

5. Problem 2021. Is this triangle right-angled?

1
2
3
4
5
6
7
8
function flag = isRightAngled(a, b, c)
tol = 1e-5;
if abs(a^2+b^2-c^2)<tol
flag = true;
else
flag = false;
end
end

6. Problem 2020. Area of an Isoceles Triangle

1
2
3
4
5
function A = isocelesArea(x,y)
w = y/2;
h = sqrt(x^2-w^2);
A = w*h;
end

7. Problem 2018. Side of a rhombus

1
2
3
function y = rhombus_side(x)
y = sqrt(x.^2 + (x+1).^2)/2;
end
Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2022-2024 CPY
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信