CODY Contest 2020 Basics - Binary Logic全10题

第一题 Problem 15. Find the longest sequence of 1's in a binary sequence.

Given a string such as

s = '011110010000000100010111'

find the length of the longest string of consecutive 1's. In this example, the answer would be 4.

Example:

Input x = '110100111'

Output y is 3

在只有‘0’和‘1’的字符串x中求‘1’连续出现的最大长度,由于只存在‘1’和‘0’,可以利用‘0’对x进行分割,得到每个1连续出现的子字符串,并存放在元组内,然后依次遍历子字符串获取长度即可(元组索引需要使用{})

function y = lengthOnes(x)
    x=strsplit(x,'0');
    y=0;
    for i=1:length(x)
        y=max(y,length(x{i}));
    end
end

 第二题 Problem 2522. Convert given decimal number to binary number.

Convert given decimal number to binary number.

Example x=10, then answer must be 1010.

将输入的十进制数转为二进制数。

不想麻烦的可以直接使用dec2bin将输入的十进制转为二进制,但输出为字符串,再使用str2num即可。

function y = dec_bin(x)
  y = str2num(dec2bin(x));
end

第三题 Problem 2678. Find out sum and carry of Binary adder

Find out sum and carry of a binary adder if previous carry is given with two bits (x and y) for addition.

Examples

Previous carry is 1 and x=y=1. Addition is 1 and carry is also 1.

Previous carry is 0 and x=y=1. Addition is 0 and carry is also 1.

Previous carry is 0 and x=1, y=0. Addition is

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值