CODY Contest 2020 Basics - Binary Logic全10題

Type真是太帥了發表於2020-12-05

第一題 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

 

相關文章