博客
关于我
【题解】【循环】完数
阅读量: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 rdflib可传递查询
查看>>
Redis 配置高可用和搭建集群
查看>>
python redis 集群_python 搭建redis集群
查看>>
python redis连接,在Python中使用Redis连接池的正确方法
查看>>
python regex_Python RegEx
查看>>
python requests post 中文结果请求得到unicode
查看>>
Python Requests接口自动化测试实战
查看>>
Python requests模块
查看>>
python request与grequests该如何选择
查看>>
python request模块
查看>>
Python requirements.txt的使用方法
查看>>
Python REST(Web 服务)框架的推荐?
查看>>
Python rsa 加密
查看>>
Python RSA操作
查看>>
Python轻松实现统计学中重要的相关性分析
查看>>
Python scrapy 常见问题及解决 【遇到的坑】
查看>>
Python Seborn热图数据的动态更新
查看>>
Python Seborn绘制空白直方图
查看>>
Python Selenium - 获取href值
查看>>
Python Selenium实现自动化测试及Chrome驱动使用!
查看>>