Java 输出所有的水仙花数

「爱情、让人受尽委屈。」 2022-02-03 11:51 361阅读 0赞

输出所有的水仙花数(三位数,各位数字的立方和等于自身)

  1. public class Test {
  2. public static void main(String[] args) {
  3. int m;
  4. System.out.print("所有的水仙花数为:");
  5. for (int n = 100; n < 1000; n++) {
  6. m = (int) Math.pow(n / 100, 3) + (int) Math.pow(n / 10 % 10, 3) + (int) Math.pow(n % 10, 3);
  7. if (n == m) {
  8. System.out.print(n + " ");
  9. }
  10. }
  11. }
  12. }

发表评论

表情:
评论列表 (有 0 条评论,361人围观)

还没有评论,来说两句吧...

相关阅读