zoukankan      html  css  js  c++  java
  • JDOJ 1140: 完数

    JDOJ 1140: 完数

    题目传送门

    Description

    一个数如果恰好等于它的因子之和,这个数就称为"完数"。 例如,6的因子为1、2、3,而6=1+2+3,因此6是"完数"。 编程序找出N之内的所有完数,并按下面格式输出其因子:

    Input

    N

    Output

    ? its factors are ? ? ?

    Sample Input

    1000

    Sample Output

    6 its factors are 1 2 3 28 its factors are 1 2 4 7 14 496 its factors are 1 2 4 8 16 31 62 124 248

    题解:

    一道很简单的模拟,涉及到的基础操作是判断质数和质因数分解。

    可以这样想,完数需要满足两个条件,第一,有着一堆质因子,第二,质因子相加等于原数。

    所以我们就可以得出思路:分别维护质因子序列(开数组),并且把每次分解出来的质因子相加,如果等于(符合条件),就可以输出。

    注意,卡格式是JDOJ的光荣传统,所以我们最后一定要加格式特判。

    代码:

    #include<cstdio>
    #include<cmath>
    #include<iostream>
    #include<cstring>
    using namespace std;
    int main()
    {
        int n;
        cin>>n;
        int tmp;
        int ram[10001];
        int cnt;
        for(int i=1;i<=n;i++)
        {
            cnt=0;
            tmp=0;
            for(int j=1;j<=i/2;j++)
                if(i%j==0)
                {
                    ram[++cnt]=j;
                    tmp+=j;
                }
            if(tmp==i)
            {
                printf("%d its factors are ",i);
                for(int j=1;j<=cnt;j++)
                    printf("%d ",ram[j]);
                cout<<endl;
            }
        }
        return 0;
    }
    
  • 相关阅读:
    合并指定表格指定列的相同文本的相邻单元格
    [转载]>/dev/null 2>&1 含义
    有关cron
    jQuery版本对checkbox影响
    c# 如何获取项目的根目录
    Javascript 字符串组装用函数 format
    sql server 删除数据库
    说说接口封装
    有开放的接口!!!!
    支付宝支付功能的集成
  • 原文地址:https://www.cnblogs.com/fusiwei/p/11294003.html
Copyright © 2011-2022 走看看