MATLAB求解水仙花数:代码及结果展示
以下是使用MATLAB计算出的全部水仙花数:\n\nmatlab\nflower_nums = [];\nfor num = 100:999\ndigit1 = floor(num/100);\ndigit2 = floor(mod(num, 100)/10);\ndigit3 = mod(num, 10);\nif (digit1^3 + digit2^3 + digit3^3) == num\n flower_nums = [flower_nums, num];\nend\nend\n\nflower_nums\n\n\n输出结果:\nmatlab\nflower_nums =\n\n 153 370 371 407\n\n\n因此,全部水仙花数为153、370、371和407。
原文地址: https://www.cveoy.top/t/topic/pKmE 著作权归作者所有。请勿转载和采集!