Advertisement

上海计算机学会2023年3月月赛C++丙组T2约数的分类

阅读量:
题目描述

西方古代数学家Nicomachus根据其真因数之和与其自身大小之间的关系对整数进行了分类

  • 当一个整数的所有正因数值之和超过该数值自身时,则被称作过剩数字(Abundant)
  • 当一个整數的所有正因数值之和低於該數值自身時,则被稱作不充足數字(Deficient)
  • 等於該數值自身的正因数值之總和時,则被稱作完美數字(Perfect)

定义a的真因数值为所有严格小于a且能整除a的正整数值。对于任意给定的正整数值n,请判定其是否属于过剩类、不足类或完美类。

输入格式

单个整数:表示给定的数字。

输出格式

根据输入整数的分类,输出 AbundantDeficientPerfect

数据范围
  • 对于 50%50% 的分数,1≤n≤1,000,000
  • 对于 100%100% 的分数,1≤n≤2,000,000,000
样例数据

输入:

6

输出:

Perfect

说明:

6=1+2+3

输入:

7

输出:

Deficient

说明:

7是素数只有一个真因子1

输入:

12

输出:

Abundant

说明:

1+2+3+4+6>12

题解

本题关键点:枚举因子。 代码如下。

复制代码
 #include <iostream>

    
 #include <cmath>
    
 using namespace std;
    
 int main(){
    
 	long long n,ans = 1;
    
 	cin>>n;
    
 	for (int i = 2; i <= sqrt(n); i++){
    
 		if (n % i == 0){
    
 			ans+=i+(n/i);
    
 		}
    
 	}
    
 	if((int)(sqrt(n))*(int)(sqrt(n))==n){
    
 		ans-=sqrt(n);
    
 	}
    
 	if(ans<n){
    
 		cout << "Deficient" << endl;
    
 	}
    
 	else if(ans==n){
    
 		cout << "Perfect" << endl;
    
 	}
    
 	else{
    
 		cout << "Abundant" << endl;
    
 	}
    
 	return 0;
    
 }

全部评论 (0)

还没有任何评论哟~