下面是用MATLAB编写的程序来输出全部水仙花数:

for num = 100:999
    digit1 = floor(num/100); % 获取百位数字
    digit2 = floor(mod(num, 100)/10); % 获取十位数字
    digit3 = mod(num, 10); % 获取个位数字
    
    if num == digit1^3 + digit2^3 + digit3^3
        disp(num);
    end
end

运行以上程序,将会输出全部的水仙花数:

153
370
371
407
用MATLAB编码:一个三位整数各位数字的立方和等于该数本身则称该数为水仙花数。输出全部水仙花数。

原文地址: https://www.cveoy.top/t/topic/hZqq 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录