CODY Contest 2020 Introduction to MATLAB 全24题(上)

 

第一题        Problem 8. Add two numbers

Given a and b, return the sum a+b in c.

返回两数之和。

function c = add_two_numbers(a,b)
  c = a+b;
end

第二题       Problem 3.Find the sum of all the numbers of the input vector

Find the sum of all the numbers of the input vector x.

Examples:

 

Input x = [1 2 3 5]

Output y is 11

 

Input x = [42 -1]

Output y is 41

返回向量所用元素之和,直接使用sum()即可。 

function ans = vecsum(x)
   sum(x)
end

注意,原本给的函数的返回变量为y,在此修改为ans,并在计算最终结果时不加分号,能够节省变量空间,很多题都可以这么优化。

第三题  Problem 1702. Maximum value in a matrix

Find the maximum value in the given matrix.

For example, if

A = [1 2 3; 4 7 8; 0 9 1];

then the answer is 9.

返回给定矩阵的最大值。测试用例最大为二维矩阵,两层max即可。

function ans = your_fcn_name(x)
    max(max(x))
end

第四题  Problem 1545. Return area of square

Side of square=input=a

Area=output=b

函数输入为正方形的边长a,范围其面积b。(看评论区有人被input误导,然后用了input获得手动输入值)

直接:

function b = area_square(a)
  b = a*a;
end

或b=a^2;即可

第五题  Problem 233. Reverse the vector

Reverse the vector elements.

Example:

Input x = [1,2,3,4,5,6,7,8,9]

Output y = [9,8,7,6,5,4,3,2,1]

向量的元素反过来,也就是第一个元素和最后一个元素互换位置,第二个和倒数第二个,以此类推,可直接使用镜像函数fliplr:

function y = reverseVector(x)
  y = fliplr(x);
end

或通过反向遍历:y=x(end:-1:1);实现,后者速度更快。

第六题 Problem 1035. Generate a vector like 1,2,2,3,3,3,4,4,4,4

Generate a vector like 1,2,2,3,3,3,4,4,4,4

So if n = 3, then return

[1 2 2 3 3 3]

And if n = 5, then return

[1 2 2 3 3 3 4 4 4 4 5 5 5 5 5]

给定输入值n,返回按照1个1、2个2、3个3……n个n顺序生成的向量。直接使用repelem函数即可,或者其他方法比如遍历等等,后者写起来比较麻烦。

function ans = your_fcn_name(n)
 repelem(1:n,1:n)
end

 第七题  Problem 23. Finding Perfect Squares

Given a vector of numbers, return true if one of the numbers is a square of one of the other numbers. Otherwise return false.

Example:

Input a = [2 3 4]

Output b is true

Output is true since 2^2 is 4 and both 2 and 4 appear on the list.

判断向量内的元素是否满足同时存在某个值和这个值的平方同时存在,题意中说明为one of ... the other ,也就说说这个值和这个值的平方不能是同一个数,所以1和0不能被考虑。

简单的思路有两种,第一种是对x和x.^2求交集,如果不为空那么返回true,或者两次遍历x,是否满足x(i)=x(j)^2。

但是由于上边所说的1和0,所以再交集中对于只出现了1次的1或0就删掉,多以可以保留,说明x中有多个1或0,满足了the other 的条件;两次遍历的话只需要在i!=j的条件下进行即可。

但是测试用例中对于1是true的:

如果想pass的话,那就不用考虑这么多,直接:

function b = isItSquared(x)
    b=~(isempty(intersect(x.^2,x)));
end

但我认为应该修改为才满足他的题意:

function b = isItSquared(x)
    b=intersect(x.^2,x);
    f=find(b==0);
    b(f(1))=[];
    f=find(b==1);
    b(f(1))=[];
    b=~isempty(b);
end

用b()=[]来删除元素,这样后边的元素会前移,为了防止出现多个1或0的情况,所以只删一个。

第八题 Problem 2. Make the vector [1 2 3 4 5 6 7 8 9 10]

这道题要求返回1到10,而不是1到n。

function x = oneToTen
  x = 1:10;
end

第九题 Problem 5. Triangle Numbers

Triangle numbers are the sums of successive integers. So 6 is a triangle number because

6 = 1 + 2 + 3

which can be displayed in a triangular shape like so

*

* *

* * *

Thus 6 = triangle(3). Given n, return t, the triangular number for n.

Example:

Input n = 4

Output t is 10

求n层三角分布的点的个数,第i层为i,也就是1,2,3.,。。。n的求和,直接用求和公式:

function t = triangle(n)
 t=n*(n+1)/2;
end

 第十题 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,直接用量子力学,不,勾股定理求解:

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

第十一题  Problem 6. Select every other element of a vector

Write a function which returns every other element of the vector passed in. That is, it returns the all odd-numbered elements, starting with the first.

Examples:

Input x = [1 3 2 4 3 5]

Output y is [1 2 3]

 

 

Input x = [5 9 3 2 2 0 -1]

Output y is [5 3 2 -1]

返回向量的所有第奇数(odd)个元素,下标从1开始,直接1:2:end即可,2代表遍历步长,如果是偶数,就是2:2:end。

function y = everyOther(x)
  y = x(1:2:end);
end

 第十二题 Problem 7. Column Removal

Remove the nth column from input matrix A and return the resulting matrix in output B.

So if

A = [1 2 3;

4 5 6];

and

n = 2

then B is

[ 1 3

4 6 ]

删除矩阵A的第n列 

第一种方法,将前1~n-1和n+1到最后两个子矩阵进行合并:

function B = column_removal(A,n)
  B=[A(:,1:n-1),A(:,n+1:end)];
end

第二种做法,置第n列为空:

B=A;
B(:,n)=B;

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值