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 1 and carry is also 0.

之前有进位pc,求当前位x+y后的进位和 当前位的结果。

当前位有三个值,x y 和pc,首先将他们相加,因为是二进制表示,如果x+y+pc的值大于等于2,那么就会向下一位进1(carry=1),并且当前位的值减去2,否则carry等于0.简化为carry等于是否pc+x+y大于等于2;sum值等于pc+x+y除以2的余数。

function [sum, carry] = bin_sum_carry(pc,x,y)
    sum=mod(x+y+pc,2);
    carry=x+y+pc>=2;  
end

第四题 Problem 34. Binary numbers

Given a positive, scalar integer n, create a (2^n)-by-n double-precision matrix containing the binary numbers from 0 through 2^n-1. Each row of the matrix represents one binary number. For example, if n = 3, then your code could return

>> binary_numbers(3)

ans =

1 1 1

0 0 0

0 1 1

0 1 0

0 0 1

1 0 0

1 1 0

1 0 1

The order of the rows does not matter.

给定n,返回2^n * n大小的矩阵,每行代表一个0~2^n-1的二进制数,根据文案对于n=3的样例,二进制数的位置似乎是任意的,不用按照大小排列。

但是按照顺序写的话可能更加如意,本题思路如下:对于n等于3和4来说,从大到小排列如下:

可以看出第一列的前半部分全为1,第二列的1-4、9-12为1……可以得出,从第一列到最后一列,1连续出现的长度从2^(n-1)不断以1/2的倍数缩小到2,如果把连续的1或0看作是一个块的话,那么从第一列到最后一列的块数从2到2^n以2倍速增长。首先zeros创建矩阵,假设现在是第i列,那么1的块长是2^(n-i),1和0的块数是2^i,当j为奇数时,第j块为1,因为用zeros创建,所以不需要管0。所以矩阵A第i列第j块1的位置为A((j-1)*batch_size+1:j*batch_size,i),batch_size表示块长。

function A = binary_numbers(n)
    A=zeros(2^n,n);
    for i=1:n
        batch_size=2^n/2^i;
        batch_num=2^i;
        for j=1:batch_num
            if mod(j,2)==1
                A((j-1)*batch_size+1:j*batch_size,i)=1;
            end
        end
    end
end

当然,也可以创建向量1~2^n-1,然后分别转为二进制,然后保存在每行中。

第五题 Problem 108. Given an unsigned integer x, find the largest y by rearranging the bits in x

Given an unsigned integer x, find the largest y by rearranging the bits in x.

Example:

Input x = 10

Output y is 12

since 10 in binary is 1010 and 12 in binary is 1100.

给定无符号数x,求对x的二进制的数值重新排列后的最大十进制数,比如x为10,那么二进制位1010,重排列后最大位1100,十进制为0.

可以转二进制,然后从大到小排列(重排列最大的二进制一定是前边全是1),然后再转到十进制即可。

function y = maxit(x)
    x=dec2bin(x);
    y=sort(x,'descend');
    y=bin2dec(y);
end

第六题 Problem 1295. Bit Reversal

Given an unsigned integer x, convert it to binary with n bits, reverse the order of the bits, and convert it back to an integer.

给定无符号数,将其转为n位的二进制数(前边补0),然后位进行翻转,在返回十进制。比如x=11 n=5,所以二进制为01011,翻转为11010,值为26.(最开始看成反转了,即将每位的0换成1,1换成0)

先转为二进制,看看字符串的长度是否小于n,如果小于n那么前遍补n-length(x)个0:x=[char(zeros(1,n-length(x))+48),x];  x表示字符串。

然后用flipdim翻转后转回十进制。

function y = bit_reverse(x,n)
    x=dec2bin(x);
    if length(x)<n
        x=[char(zeros(1,n-length(x))+48),x];
    end
    y=bin2dec(flipdim(x,2));
end

第七题 Problem 1547. Relative ratio of "1" in binary number

Input(n) is positive integer number

Output(r) is (number of "1" in binary input) / (number of bits).

Example:

  • n=0; r=0
  • n=1; r=1
  • n=2; r=0.5

统计给定十进制数的二进制中‘1’的占比。

将n转为二进制后统计‘1’的次数与长度之比即可。

function r = ones_ratio(n)
  n=dec2bin(n);
  r=sum(n=='1')/length(n);
end

第八题 Problem 2891. Binary code (array)

Write a function which calculates the binary code of a number 'n' and gives the result as an array(vector).

Example:

Input n = 123

Output y =[1,1,1,1,0,1,1]

编写一个函数将n转为二进制并用向量存储,第一种就是用dec2bin后,将字符转为数字, 但是用太多了不好,这次自己试着写写吧。

递归,每次取x除以2的余数放入向量尾部,然后x=x/2,直到x=0,然后将向量翻转。

function y = dectobin(x)
  i=1;
    while x>0
       y(i)=mod(x,2);
       i=i+1;
       x=floor(x/2);
    end
    y=flipdim(y,2);
end

第九题 Problem 43977. Converting binary to decimals

Convert binary to decimals.

Example:

010111 = 23.

110000 = 48.

将二进制数转为十进制。

设y=0,取x的最后一位,y=y+最后一位乘以2的i-1次方,i表示第j次计算,然后取x除以10的向下取整,提交了才发现测试用例的x为字符串,字符串的话直接从后向前遍历,都不用取余除10.懒得改成字符串的方法了,直接在最前边将字符串转为数字。

function y = bin_to_dec(d)
    d=str2num(d);
    y=0;
    i=0;
    while d>0
       y=y+mod(d,10)*2^i;
       i=i+1;
       d=floor(d/10);
    end
end

第十题 Problem 2869. There are 10 types of people in the world

Those who know binary, and those who don't.

The number 2015 is a palindrome in binary (11111011111 to be exact) Given a year (in base 10 notation) calculate how many more years it will be until the next year that is a binary palindrome. For example, if you are given the year 1881 (palindrome in base 10! :-), the function should output 30, as the next year that is a binary palindrome is 1911. You can assume all years are positive integers.

Good luck!!kcul dooG

世界上有10种人,一种是懂二进制的人,另一种是不懂二进制的人。

2015的二进制(11111011111)是回文,给定十进制年份,判断下一个为回文的年份,比如1881年的下一个回文年份为1911,所以返回他们的年份之差30。听起来是二进制和回文的结合。似乎并没有找到一个快速的方法,只能逐年地判断是否是回文。先将x转为二进制,判断是否是回文,如果是则结束,否则y+1,并且在二进制字符串x尾部+1,然后对加1后的进行进位,得到了下一年的二进制,而不需要重复地计算每年的二进制。

function y = yearraey(x)
    x=dec2bin(x);
    y=0;
    while 1
        ret = 1;%判断是否为回文
        for i=1:length(x)/2
            if (x(i)~=x(end-i+1))
                ret=0;
                break;
            end
        end
        if ret%是回文 结束
            return;
        end
        y=y+1;%否则 +1
        x(end)=x(end)+1;
        for i=length(x):-1:1%对x进行进位
            if x(i)=='2' %需要进位
                x(i)='0';
                if i>1
                  x(i-1)=x(i-1)+1;
                else
                  x=['1',x];
                  break;
                end
            else %直到不需要进位
                break;
            end
        end            
    end
end

猜你喜欢

转载自blog.csdn.net/qq_36614557/article/details/110679111