博客
关于我
【题解】【循环】完数
阅读量: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/

你可能感兴趣的文章
npm install CERT_HAS_EXPIRED解决方法
查看>>
npm install digital envelope routines::unsupported解决方法
查看>>
npm install 卡着不动的解决方法
查看>>
npm install 报错 EEXIST File exists 的解决方法
查看>>
npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
查看>>
npm install 报错 Failed to connect to github.com port 443 的解决方法
查看>>
npm install 报错 fatal: unable to connect to github.com 的解决方法
查看>>
npm install 报错 no such file or directory 的解决方法
查看>>
npm install 权限问题
查看>>
npm install报错,证书验证失败unable to get local issuer certificate
查看>>
npm install无法生成node_modules的解决方法
查看>>
npm install的--save和--save-dev使用说明
查看>>
npm node pm2相关问题
查看>>
npm run build 失败Compiler server unexpectedly exited with code: null and signal: SIGBUS
查看>>
npm run build报Cannot find module错误的解决方法
查看>>
npm run build部署到云服务器中的Nginx(图文配置)
查看>>
npm run dev 和npm dev、npm run start和npm start、npm run serve和npm serve等的区别
查看>>
npm run dev 报错PS ‘vite‘ 不是内部或外部命令,也不是可运行的程序或批处理文件。
查看>>
npm scripts 使用指南
查看>>
npm should be run outside of the node repl, in your normal shell
查看>>