题目:
   
给定 n 个正整数 ai,对于每个整数 ai,请你按照从小到大的顺序输出它的所有约数。
    输入格式
    
    第一行包含整数 n。
   
接下来 n 行,每行包含一个整数 ai。
    输出格式
    
    输出共 n 行,其中第 i 行输出第 i 个整数 ai 的所有约数。
   
    数据范围
    
    1≤n≤100,
    
    2≤ai≤2×109
    
    输入样例:
    
    2
    
    6
    
    8
    
    输出样例:
    
    1 2 3 6
    
    1 2 4 8
   
    
    
    代码:
   
#include<iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> get_divisors(int n)
{
    vector<int> res;
    for(int i = 1; i <= n/i; i++)//枚举1~sqrt(n)中的数字
        if(n % i == 0)//如果n能被i整除,那么把i和x/i添加到数组中,需要判重,最后对数组排个序
        {
            res.push_back(i);
            if(i != n/i)
                res.push_back(n/i);
        }
    sort(res.begin(),res.end());
    return res;
}
int main()
{
    int n;
    cin>>n;
    while(n--)
    {
        int x;
        cin>>x;
        auto res = get_divisors(x);
        for(auto t : res)
            cout<<t<<' ';
        cout<<endl;
    }
    return 0;
}
 
版权声明:本文为Hunter_Kevin原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
