题目描述
时间超了 两个循环的原因吧
样例
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
int i,n,j,num,sum;
cin>>n;
for(i=0;i<n;i++){
cin>>num;
sum=0;
for(j=1;j<num;j++){
if(num%j==0)sum+=j;
}if(num==sum)printf("%d is perfect\n",num);
else printf("%d is not perfect\n",num);
}
return 0;
}
算法1
(暴力枚举) $O(n^2)$
blablabla
涉及质数的一定记得用sqrt优化下
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int main(){
int i,n,j,num,sum;
cin>>n;
for(i=0;i<n;i++){
cin>>num;
sum=1;
for(j=2;j<=sqrt(num);j++){
if(num%j==0&&j!=sqrt(num))sum+=j+num/j;
if(num%j==0&&j==sqrt(num))sum+=j;
}if(num==sum&&num!=1)printf("%d is perfect\n",num);
else printf("%d is not perfect\n",num);
}
return 0;
}
算法2
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
blablabla