#include <iostream>
#include <vector>
#include <algorithm> // for sort
using namespace std;
int main() {
int n;
cin >> n;
vector<int> factors; // 存儲 n 的因數
int factor_sum = 0; // n 的因數和
for (int i = 1; i < n; i++) {
if (n % i == 0) {
factors.push_back(i);
factor_sum += i;
}
}
sort(factors.begin(), factors.end()); // 排序
for (int i = 0; i < factors.size(); i++) {
if (i != 0) {
cout << "+";
}
cout << factors[i];
}
cout << "=" << factor_sum << endl;
if (factor_sum == n) { // 如果 n 是完全數
cout << n << " is perfect." << endl;
} else { // 否則尋找友好數
vector<int> friend_factors; // 存儲 m 的因數
int friend_sum = 0; // m 的因數和
for (int i = 1; i < factor_sum; i++) {
if (factor_sum % i == 0) {
friend_factors.push_back(i);
friend_sum += i;
}
}
sort(friend_factors.begin(), friend_factors.end()); // 排序
for (int i = 0; i < friend_factors.size(); i++) {
if (i != 0) {
cout << "+";
}
cout << friend_factors[i];
}
cout << "=" << friend_sum << endl;
if (friend_sum == n) { // 如果找到了友好數 m
cout << n << " and " << factor_sum << " are friends." << endl;
} else { // 沒有找到友好數
cout << n << " has no friends." << endl;
}
}
return 0;
}
#include <iostream>
#include <vector>
#include <algorithm> // for sort
using namespace std;
int main() {
int n;
scanf("%d", &n);
vector<int> factors; // 存儲 n 的因數
int factor_sum = 0; // n 的因數和
for (int i = 1; i < n; i++) {
if (n % i == 0) {
factors.push_back(i);
factor_sum += i;
}
}
sort(factors.begin(), factors.end()); // 排序
for (int i = 0; i < factors.size(); i++) {
if (i != 0) {
printf("+");
}
printf("%d", factors[i]);
}
printf("=%d\n", factor_sum);
if (factor_sum == n) { // 如果 n 是完全數
printf("%d is perfect.\n", n);
} else { // 否則尋找友好數
vector<int> friend_factors; // 存儲 m 的因數
int friend_sum = 0; // m 的因數和
for (int i = 1; i < factor_sum; i++) {
if (factor_sum % i == 0) {
friend_factors.push_back(i);
friend_sum += i;
}
}
sort(friend_factors.begin(), friend_factors.end()); // 排序
for (int i = 0; i < friend_factors.size(); i++) {
if (i != 0) {
printf("+");
}
printf("%d", friend_factors[i]);
}
printf("=%d\n", friend_sum);
if (friend_sum == n) { // 如果找到了友好數 m
printf("%d and %d are friends.\n", n, factor_sum);
} else { // 沒有找到友好數
printf("%d has no friends.\n", n);
}
}
return 0;
}
都做到這樣了 最後一個測資依舊給我TLE
#include
#include
#include // for sort
using namespace std;int main() {
int n;
cin >> n;
vector factors; // 存儲 n 的因數
int factor_sum = 0; // n 的因數和
for (int i = 1; i < n; i++) {
if (n % i == 0) {
factors.push_back(i);
factor_sum += i;
}
}sort(factors.begin(), factors.end()); // 排序
for (int i = 0; i < factors.size(); i++) {
if (i != 0) {
cout << "+";
}
cout << factors[i];
}
cout << "=" << factor_sum << endl;if (factor_sum == n) { // 如果 n 是完全數
cout << n << " is perfect." << endl;
} else { // 否則尋找友好數
vector friend_factors; // 存儲 m 的因數
int friend_sum = 0; // m 的因數和
for (int i = 1; i < factor_sum; i++) {
if (factor_sum % i == 0) {
friend_factors.push_back(i);
friend_sum += i;
}
}sort(friend_factors.begin(), friend_factors.end()); // 排序
for (int i = 0; i < friend_factors.size(); i++) {
if (i != 0) {
cout << "+";
}
cout << friend_factors[i];
}
cout << "=" << friend_sum << endl;if (friend_sum == n) { // 如果找到了友好數 m
cout << n << " and " << factor_sum << " are friends." << endl;
} else { // 沒有找到友好數
cout << n << " has no friends." << endl;
}
}return 0;
}
因數找到平方根就好,會快很多
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm> // for sort
using namespace std;int main() {
int n;
scanf("%d", &n);vector<int> factors; // 存儲 n 的因數
int factor_sum = 0; // n 的因數和
double range = sqrt(n);
int buffer;
for (int i = 1; i < range; i++) {
if (n % i == 0) {
factors.push_back(i);
factor_sum += i;
buffer = n / i;
factors.push_back(buffer);
factor_sum += buffer;
}
} factor_sum -= n;range = (int)range;
if (range * range == n) {
factors.push_back(range);
factor_sum += range;
}sort(factors.begin(), factors.end()); // 排序
factors.erase(factors.end() - 1);
for (int i = 0; i < factors.size(); i++) {
if (i != 0) {
printf("+");
}
printf("%d", factors[i]);
}
printf("=%d\n", factor_sum);if (factor_sum == n) { // 如果 n 是完全數
printf("%d is perfect.\n", n);
} else { // 否則尋找友好數
vector<int> friend_factors; // 存儲 m 的因數
int friend_sum = 0; // m 的因數和
range = sqrt(factor_sum);
for (int i = 1; i < range; i++) {
if (factor_sum % i == 0) {
friend_factors.push_back(i);
friend_sum += i;
buffer = factor_sum / i;
friend_factors.push_back(buffer);
friend_sum += buffer;
}
} friend_sum -= factor_sum;range = (int)range;
if (range * range == factor_sum) {
friend_factors.push_back(range);
friend_sum += range;
}sort(friend_factors.begin(), friend_factors.end()); // 排序
friend_factors.erase(friend_factors.end() - 1);
for (int i = 0; i < friend_factors.size(); i++) {
if (i != 0) {
printf("+");
}
printf("%d", friend_factors[i]);
}
printf("=%d\n", friend_sum);if (friend_sum == n) { // 如果找到了友好數 m
printf("%d and %d are friends.\n", n, factor_sum);
} else { // 沒有找到友好數
printf("%d has no friends.\n", n);
}
}return 0;
}