Files
python/TangDou/LuoGuBook/21_2.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

29 lines
648 B
C++

#include <bits/stdc++.h>
using namespace std;
//找出n的所有约数
const int N = 1010;
int a[N]; //哪些约数
int idx; //共多少个
void find_divisors(int n) {
for (int i = 1; i <= n / i; i++)
if (n % i == 0) {
a[++idx] = i;
//防止出现5*5这样的录入两次
if (i != n / i)a[++idx] = n / i;
}
}
int main() {
//找一下
find_divisors(120);
//输出个数
cout << idx << endl;
//输出是哪些
sort(a + 1, a + 1 + idx);
for (int i = 1; i <= idx; i++) cout << a[i] << " ";
cout << endl;
return 0;
}