码迷,mamicode.com
首页 > 编程语言 > 详细

Java经典编程题50道之九

时间:2017-06-01 13:06:37      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:example   因子   int   main   i++   oid   str   class   public   

一个数如果恰好等于它的因子之和,这个数就称为"完数"。例如6=1+2+3。编程找出1000以内的所有完数。

public class Example09 {
    public static void main(String[] args) {
        number();
    }

    public static void number() {
        int count = 0;
        for (int i = 1; i <= 1000; i++) {
            int t = 0;
            for (int j = 1; j <= i / 2; j++) {
                if (i % j == 0) {
                    t = t + j;
                }
            }
            if (t == i) {
                System.out.print(i + "\t");
                count++;
            }
        }
        System.out.println("\n共有" + count + "个完数。");
    }
}

Java经典编程题50道之九

标签:example   因子   int   main   i++   oid   str   class   public   

原文地址:http://www.cnblogs.com/qubo520/p/6928075.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!