求下一个阿姆斯特朗数 matlab
来源:学生作业帮 编辑:搜搜做题作业网作业帮 分类:综合作业 时间:2024/07/28 14:27:42
求下一个阿姆斯特朗数 matlab
阿姆斯特朗数就是各位数字立方和等于其本身,用matlab实现,我的代码如下:
function c = AmsNum_cxd(n)
c=n;
b = panduan(c);
while(b==0)
c= c+1;
b = panduan(c);
end
function b = panduan(n)
sum = 0;
t = (ceil(log10(n))-1);
for i = 1:t
sum = sum + ((mod (n,10^i)-mod(n,10^(i-1)))/10^(i-1))^3;
end
if (sum == n)
b=1;
else b= 0 ;
end
但是有问题,我也是不知道哪里错了,就是调用的时候不出现结果
阿姆斯特朗数就是各位数字立方和等于其本身,用matlab实现,我的代码如下:
function c = AmsNum_cxd(n)
c=n;
b = panduan(c);
while(b==0)
c= c+1;
b = panduan(c);
end
function b = panduan(n)
sum = 0;
t = (ceil(log10(n))-1);
for i = 1:t
sum = sum + ((mod (n,10^i)-mod(n,10^(i-1)))/10^(i-1))^3;
end
if (sum == n)
b=1;
else b= 0 ;
end
但是有问题,我也是不知道哪里错了,就是调用的时候不出现结果
![求下一个阿姆斯特朗数 matlab](/uploads/image/z/17847505-1-5.jpg?t=%E6%B1%82%E4%B8%8B%E4%B8%80%E4%B8%AA%E9%98%BF%E5%A7%86%E6%96%AF%E7%89%B9%E6%9C%97%E6%95%B0+matlab)
应该是t = ceil(log10(n));你的程序可以简洁些:
function c = AmsNum_cxd(n)
while 1
s = sprintf('%d', n)-'0';
if sum(s.^3) == n
c = n;
return;
end
n = n+1;
end
end
function c = AmsNum_cxd(n)
while 1
s = sprintf('%d', n)-'0';
if sum(s.^3) == n
c = n;
return;
end
n = n+1;
end
end