开根号有可能是小数,也有可能是整数
当开出整数时,+i和+x/i算了两次,需要减掉
#include<iostream> #include<vector> #include<math.h> using namespace std; int getSumOfFactor(int x){ int sum = 1; int root = sqrt(x); for(int i=2;i<=root;i++){ if(x%i==0){ sum += i; sum += x/i; } } if(root*root==x) sum-=root; return sum; } int main(){ int n; while(cin>>n){ bool printed = false; for(int i=1;i<=n;i++){ if(i==getSumOfFactor(i)){ if(!printed){ cout<<i; printed = true; } else { cout<<" "<<i; } } } cout<<endl; } return 0; }