用试除法求一个数的所有约数

题目:
在这里插入图片描述
代码如下:

#include<iostream>
#include<algorithm>
#include<vector>

using namespace std;

const int N = 110;

int n;
vector<int> res;

void get_divisor(int x){
    
    
    for(int i = 1; i <= x / i; i++){
    
    
        if(x % i == 0){
    
    
            res.push_back(i);
            if(i != x / i) res.push_back(x / i);
        }
        
    }
    sort(res.begin(), res.end());
}


int main(){
    
    
    cin >> n;
    while(n --){
    
    
        int x;
        cin >> x;
        get_divisor(x);
        
        for(auto i:res) cout << i << ' ';
        cout << endl;
        res.clear();
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_44879626/article/details/108061580