AcWing 725. 完全数
原题链接
中等
作者:
古娜拉黑暗之神
,
2021-02-07 16:20:46
,
所有人可见
,
阅读 305
第一种方法,正确
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int main(){
int num;
cin >> num;
int x;
// bool flag = false;
while(num > 0){
cin >> x;
int t = 0;
for(int i =1;i*i<=x;i++){
if(x % i == 0){
if(i < x )
t = t+i; //平方根及平方根之前的数在这儿加
if(x/i < x && i!=x/i) //平方根的位置比较灵活
t = t+x/i; //平方根(不含)之后的数在这儿加
}
}
if(x == t){
cout<< x <<" is perfect"<<endl;
}else{
cout<< x << " is not perfect"<<endl;
}
num--;
}
return 0;
}
第二种方法 TLE超时
#include<iostream>
#include<cstdio>
using namespace std;
int main(){
int num;
cin >> num;
int x;
// bool flag = false;
while(num > 0){
cin >> x;
int t = 0;
for(int i =1;i <x;i++){
if(x % i == 0){
t = t + i;
}
} //这个题需要优化,x最大一亿,但是num最大是100,所以总计算次数是100亿次,但是一般1s内计算1亿次,所以超时。
if(x == t){
cout<< x <<" is perfect"<<endl;
}else{
cout<< x << " is not perfect"<<endl;
}
num--;
}
return 0;
}