如果整数A的全部因子(包括1,不包括A本身)之和等于B;且整数B的全部因子(包括1,不包括B本身)之和等于A,则将整数A和B称为亲密数。 求3000以内的全部亲密数。
// ConsoleApplication18.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
int yinsu(int n) {
int sum = 0;
for (int i = 1; i < n; i++) {
if (n % i == 0)
sum = sum + i;
}
return sum;
}
int main()
{
unordered_map<int, vector<int>> map;
for (int i = 2; i < 3000; i++) {
int t = yinsu(i);
map[t].push_back(i);
}
for (auto pos : map) {
if (pos.second.size() > 1) {
cout << pos.first << ":" << endl;
for (int n : pos.second)
{
cout << n << " ";
}
cout << "********************************************************" << endl;
}
}
}