java 水仙花数

来源:互联网 发布:大学社交 知乎 编辑:程序博客网 时间:2024/06/05 05:26
public static void main(String[] args) throws InterruptedException{        List list = new ArrayList();        for (int i = 100; 1<2;i++ ){            int h = i/100;            int t = (i-100*h)/10;            int a = (i-10*t-100*h);            if (Math.pow(a,3)+Math.pow(t,3)+Math.pow(h,3)==i){                list.add(i);                System.out.println(list);            }//            Thread.sleep(1000);        }    }这样是有问题的,只能读到1001,因为会有千位出现,完善后:
public static void main(String[] args) throws InterruptedException{    List list = new ArrayList();    for (int i = 100; i<100000;i++ ){        String tt = i+"";        int a =Integer.parseInt(tt.substring(tt.length()-3,tt.length()-2));        int t =Integer.parseInt(tt.substring(tt.length()-2,tt.length()-1));        int h =Integer.parseInt(tt.substring(tt.length()-1,tt.length()));        if (Math.pow(a,3)+Math.pow(t,3)+Math.pow(h,3)==i){            list.add(i);            System.out.println(list);        }    }}
[153, 370, 371, 407]
0 0
原创粉丝点击