改了C語法最後一個測資輸出也是TLE, 還有哪裡可加快?
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
long long sum_factors(int n) // 計算真因數的和
{
long long sum = 0;
for (int i = 1; i <= sqrt(n); i++)
{
if (n % i == 0)
{
sum += i;
if (i != n / i)
sum += n / i;
}
}
return sum - n;
}
void print_factors(int num) // 印出所有真因數
{
long long sum = 0;
vector<int> factors;
for (int i = 1; i <= num / 2; i++)
{
if (num % i == 0)
{
sum += i;
factors.push_back(i);
}
}
int n = factors.size();
for (int i = 0; i < n; i++)
{
cout << factors[i];
if (i < n - 1 && n > 1)
cout << "+";
}
cout << "=" << sum << '\n';
}
void check(int n) // 檢查 n 的真因數和是否為盈數、虧數或完全數
{
int sum = sum_factors(n);
if (sum == n)
{
print_factors(n);
cout << n << " is perfect."<<'\n';
}
else
{
if (sum_factors(sum) == n)
{
print_factors(n);
print_factors(sum);
cout << n << " and " << sum << " are friends." <<'\n';
}
else if(sum==1)
{
cout<<"1=1"<<'\n'<<"=0"<<'\n'<<n<<" has no friends."<<'\n';
}
else
{
print_factors(n);
print_factors(sum);
cout<<n<<" has no friends."<<'\n';
}
}
}
int main()
{
int n;
ios_base::sync_with_stdio(false);
cin.tie(0);
while (cin >> n)
{
check(n);
}
return 0;
}
改了C語法最後一個測資輸出也是TLE, 還有哪裡可加快?
#include
#include
#includeusing namespace std;
long long sum_factors(int n) // 計算真因數的和
{
long long sum = 0;
for (int i = 1; i <= sqrt(n); i++)
{
if (n % i == 0)
{
sum += i;
if (i != n / i)
sum += n / i;
}
}
return sum - n;
}void print_factors(int num) // 印出所有真因數
{
long long sum = 0;
vector factors;
for (int i = 1; i <= num / 2; i++)
{
if (num % i == 0)
{
sum += i;
factors.push_back(i);
}
}
int n = factors.size();
for (int i = 0; i < n; i++)
{
cout << factors[i];
if (i < n - 1 && n > 1)
cout << "+";
}
cout << "=" << sum << '\n';
}void check(int n) // 檢查 n 的真因數和是否為盈數、虧數或完全數
{
int sum = sum_factors(n);if (sum == n)
{
print_factors(n);
cout << n << " is perfect."<<'\n';
}
else
{
if (sum_factors(sum) == n)
{
print_factors(n);
print_factors(sum);
cout << n << " and " << sum << " are friends." <<'\n';
}
else if(sum==1)
{
cout<<"1=1"<<'\n'<<"=0"<<'\n'< }
else
{
print_factors(n);
print_factors(sum);
cout< }
}
}int main()
{
int n;
ios_base::sync_with_stdio(false);
cin.tie(0);
while (cin >> n)
{
check(n);
}
return 0;
}
因數找到平方根就好
改了C語法最後一個測資輸出也是TLE, 還有哪裡可加快?
#include
#include
#includeusing namespace std;
long long sum_factors(int n) // 計算真因數的和
{
long long sum = 0;
for (int i = 1; i <= sqrt(n); i++)
{
if (n % i == 0)
{
sum += i;
if (i != n / i)
sum += n / i;
}
}
return sum - n;
}void print_factors(int num) // 印出所有真因數
{
long long sum = 0;
vector factors;
for (int i = 1; i <= num / 2; i++)
{
if (num % i == 0)
{
sum += i;
factors.push_back(i);
}
}
int n = factors.size();
for (int i = 0; i < n; i++)
{
cout << factors[i];
if (i < n - 1 && n > 1)
cout << "+";
}
cout << "=" << sum << '\n';
}void check(int n) // 檢查 n 的真因數和是否為盈數、虧數或完全數
{
int sum = sum_factors(n);if (sum == n)
{
print_factors(n);
cout << n << " is perfect."<<'\n';
}
else
{
if (sum_factors(sum) == n)
{
print_factors(n);
print_factors(sum);
cout << n << " and " << sum << " are friends." <<'\n';
}
else if(sum==1)
{
cout<<"1=1"<<'\n'<<"=0"<<'\n'< }
else
{
print_factors(n);
print_factors(sum);
cout< }
}
}int main()
{
int n;
ios_base::sync_with_stdio(false);
cin.tie(0);
while (cin >> n)
{
check(n);
}
return 0;
}
因數找到平方根就好 謝謝提醒! 原來在印出所有真因數的地方漏了, 修改後加上排序就過了