【无标题】cody分享,希望自己能坚持分享,坚持学习matlab

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

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;
        x(end)=x(end)+1;
        for i=length(x):-1:1
            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

Problem 43977. Converting binary to decimals

function y = bin_to_dec(d)
  y = bin2dec(d);
end

Problem 2891. Binary code (array)

function y = dectobin(x)
  y = num2str(dec2bin(x))-'0';
end

Problem 1547. Relative ratio of "1" in binary number

function r = ones_ratio(x)

a=length(find(dec2bin(x)=='1'));

b=length(dec2bin(x));

r = a./b;

end

Problem 1295. Bit Reversal

function y = bit_reverse(x,n)
a=dec2bin(x);
if n==length(a)
   b=num2str(a)-'0';
else
  b=[0 num2str(a)-'0'];
end
 c=num2str(fliplr(b));
 y=bin2dec(c);
end

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

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

Problem 34. Binary numbers

function d = binary_numbers(n)
d=zeros(2^n,n);
for i=0:2^n-1
c=dec2bin(i);
b=num2str(c)-'0';
for j=1:n
if length(b)==j
    d(i+1,:)=[zeros(1,n-j) b];
end
end
end

Problem 2678. Find out sum and carry of Binary adder

function [sum, carry] = bin_sum_carry(pc,x,y)
if x==1&y==1&pc==1
    sum=1;carry=1;
elseif x==1&y==1&pc==0
    sum=0;carry=1;
elseif x==1&y==0&pc==0
    sum=1;carry=0;
else
    sum=0;carry=0;
end

Problem 2522. Convert given decimal number to binary number.

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

​​​​​​Problem 15. Find the longest sequence of 1's in a binary sequence.

function y = lengthOnes(x)
n=length(x);
a=zeros(1,n);
count=0;
for i=1:n
if find(x(i)=='1')
    count=count+1;
else
    count=0;
end
a(1,i)=[count];
end
y=max(a);
end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值