请优化一下这个代码:#include<bits/stdc++.h> using namespace std; int main(){ int n,k,sum,m=0; char a[100]={}; cin>>n>>k; for(int i=1;i<=n;++i){ sum=0; for(int j=1;j<i;++j) if(i%j==0) sum+=j; if(sum==i) a[m++]=i; } if(strlen(a)==0) cout<<"None"<<endl; else for(int j=0;j<strlen(a);++j){ printf("%d",a[j]); if(j!=strlen(a)-1) cout<<endl; } return 0; }
时间: 2024-04-19 16:25:38 浏览: 153
万能头文件#include“bitsstdc++.h”.pdf
#include <iostream>
#include <vector>
int main() {
int n, k;
std::cin >> n >> k;
std::vector<int> perfectNumbers;
for (int i = 1; i <= n; ++i) {
int sum = 0;
for (int j = 1; j < i; ++j) {
if (i % j == 0) {
sum += j;
}
}
if (sum == i) {
perfectNumbers.push_back(i);
}
}
if (perfectNumbers.empty()) {
std::cout << "None" << std::endl;
} else {
for (int j = 0; j < perfectNumbers.size(); ++j) {
std::cout << perfectNumbers[j];
if (j != perfectNumbers.size() - 1) {
std::cout << std::endl;
}
}
}
return 0;
}
阅读全文