例21_2 苹果丰收

#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;
}
原文地址:https://www.cnblogs.com/littlehb/p/15189848.html