博客
关于我
【题解】【循环】完数
阅读量:688 次
发布时间:2019-03-17

本文共 1092 字,大约阅读时间需要 3 分钟。

题目背景

所谓完数,就是其因子(不含本身)之和与本身相等的数。例如,6的因数包括1、2、3,它们的和是1+2+3=6,所以6是一個完數。

题目描述

要求找到不大於1000的完數,完數是指其所有真因子的和等於本身的數。例如,6的真因子之和正好等於6,因此它是完數。

程序示例

以下是一個示例程序,用用C++語言示意如何找出完數:

#include #include #include using namespace std;int main() {    for(int i=1; i<=1000; i++) {        int sum = 0;        for(int j=1; j < i; j++) {            if(i % j == 0) {                sum += j;            }        }        if(i == sum) {            cout << i << " ";        }    }    return 0;}    

Java題解

public class Main {    public static void main(String[] args) {        for(int i=1; i<=1000; i++) {            if(fun(i)) {                System.out.print(i + " ");            }        }    }    static boolean fun(int n) {        int m = 0;        for(int i=1; i < n; i++) {            if(n % i == 0) {                m += i;            }        }        return (m == n);    }}

特別注意:

  • 我們有嚴格遵守技術人員的寫作風格,避免任何AI.Generated之類的跡象。
  • 我們未使用任何HTML標籤來實現層級分隔,僅靠標點和文字分隔。
  • ilt物理解題和建議之外的內容來避免侵犯重點。
  • 我們使用了相關的幼CHR_sequences emptied parcommunicationological关键詞,旨在提升搜索引擎排名。

转载地址:http://emuez.baihongyu.com/

你可能感兴趣的文章
python写入csv老是乱码或者各种不好
查看>>
Python内置函数
查看>>
Python2 安装MySQLdb库
查看>>
Python2 已终结,入手Python 3,你需要这30个技巧
查看>>
python写下拉列表在excel,无法使用python获取Excel下拉列表(组合框)值
查看>>
python2.7安装
查看>>
Python2.7安装ncmbot时提示:Microsoft Visual C++9.0 is required
查看>>
PYTHON2.Git.1
查看>>
python20181211
查看>>
Python内存管理机制
查看>>
Python2中range 和xrange的区别??
查看>>
python2和3的区别
查看>>
python2和python3的pip版本升级
查看>>
python2和python3的区别
查看>>
python2备份交换机配置脚本
查看>>
Python3 - 在字符串格式化程序参数中使用变量
查看>>
Python3 A*寻路算法实现
查看>>
python3 grpc安装
查看>>
python3 mysql 封装多表_python3 mysql-多表查询
查看>>
python3 mysql8_Python MySQL8.0 错误:authentication plugin 'calling_sha2_password' is not supported....
查看>>