标签:字符串输入 mat har 并且 log 输入 for div 数字
clear all clear %% 字符串输入 a=‘12345678910111213‘; b=‘13121110987654321‘; a_len=length(a); b_len=length(b); A=zeros(1,a_len); B=zeros(1,a_len); s=zeros(1,a_len+b_len); S=zeros(1,a_len+b_len); W=zeros(1,a_len+b_len); sum_len=a_len+b_len; %% 字符串转为数组,并且翻转字符 for ii=1:a_len A(ii)=str2num(a(a_len+1-ii)); B(ii)=str2num(b(b_len+1-ii)); end %% %分块相加 for jj=1:sum_len-1 n=1; while (n<=a_len)&(n<jj+1) if n==(jj+1-n) s(jj)=s(jj)+A(n)*B(jj+1-n); elseif (jj+1-n)<=a_len s(jj)=s(jj)+A(n)*B(jj+1-n); end n=n+1; end end %% s_10=0; s_100=0; for jj=1:sum_len if jj==1 S(jj)=mod(s(1),10); W(jj)=mod(s(1),10); elseif jj==2 S(jj)=s(jj)+floor(s(jj-1)/10); W(jj)=mod(S(jj),10); else S(jj)=s(jj)+floor(S(jj-1)/10); W(jj)=mod(S(jj),10); end end %% if W(end)==0 W(end)=[] end %% str=num2str(W); str_f=strrep(str, ‘ ‘, ‘‘); final=str_f(end:-1:1)
clc clear %% a=182; b=165; a_len=floor(log10(a))+1; b_len=floor(log10(b))+1; A=zeros(1,a_len); B=zeros(1,a_len); s=zeros(1,a_len+b_len); S=zeros(1,a_len+b_len); W=zeros(1,a_len+b_len); sum_len=a_len+b_len; %% 数字转为数组,并且翻转字符 for ii=1:a-1 A(ii)=mod(a,10); a=floor(a/10); B(ii)=mod(b,10); b=floor(b/10); end A(ii+1)=a; B(ii+1)=b; %% %分块相加 for jj=1:sum_len-1 n=1; while (n<=a_len)&(n<jj+1) if n==(jj+1-n) s(jj)=s(jj)+A(n)*B(jj+1-n); else s(jj)=s(jj)+A(n)*B(jj+1-n); end n=n+1; end end %% s_10=0; s_100=0; for jj=1:sum_len if jj==1 S(jj)=mod(s(1),10); W(jj)=mod(s(1),10); elseif jj==2 S(jj)=s(jj)+floor(s(jj-1)/10); W(jj)=mod(S(jj),10); else S(jj)=s(jj)+floor(S(jj-1)/10); W(jj)=mod(S(jj),10); end end %% if W(end)==0 W(end)=[] end %% str=num2str(W); str_f=strrep(str, ‘ ‘, ‘‘); final=str_f(end:-1:1)
标签:字符串输入 mat har 并且 log 输入 for div 数字
原文地址:http://www.cnblogs.com/ruo-li-suo-yi/p/7803492.html