Analyzing two ideas matlab number palindromic

Today someone asked me how to use matlab judge palindrome, I give the following ideas

A thought: the use of mathematical models to determine

%author:zhuweijie_猪猪侠
%date:2019/09/03
%E-mail:[email protected]
%回文数
%根据数学模型判断是不是回文数
function hwshu(x)
if x>=0
   str=num2str(x);%转换字符串
    for i=1:length(str)%字符串长度
        r(i)=str2double(str(i));%再转换为双精度
    end
    if length(r)==1%判断长度=1则是回文数-
        disp('是回文数')
    else
        flag=0;
       for j=1:floor(length(r)/2)
           if r(j)==r(length(r)+1-j)%模型
              flag=flag+1;
           end
       end
       if flag==floor(length(r)/2)
           disp('是回文数')
       else
           disp('不是回文数')
       end
    end
else
    disp('参数输入错误,请重新输入')
end
end

Two ideas: the use of the digital determination of structural features palindromic

%author:zhuweijie_猪猪侠
%date:2019/09/03
%E-mail:[email protected]
%回文数
%数字特征规律来判断是不是回文数
function hws(x)
if x>=0
   str=num2str(x);%转换字符串
    for i=1:length(str)%字符串长度
        r(i)=str2double(str(i));%再转换为双精度
    end
    if length(r)==1%判断长度=1则是回文数-
        disp('是回文数')
    else
        len=floor(length(r)/2);
        %提取前一半数据
        fro_num=r(1:len);
        %提取后一半数据,然后逆转
        aft_num=fliplr(r(length(r)-len+1:length(r)));
        if fro_num==aft_num
            disp('是回文数')
        else
            disp('不是回文数')
        end
    end
else
    disp('参数输入错误,请重新输入')
end
end

 

Published 53 original articles · won praise 174 · Views 140,000 +

Guess you like

Origin blog.csdn.net/zzx2016zzx/article/details/100512117