试除法分解质因数

试除法分解质因数

#include <bits/stdc++.h>
using namespace std;

int t, x;

void divide(int n){
    for(int i = 2; i <= n/i; ++i){ // 大于sqrt(n)的质因子最多有一个
        if(n % i == 0){
            int s = 0;
            while(n % i == 0){
                n /= i;
                ++s;
            }
            printf("%d %d
", i, s);
        }
    }
    if(n > 1) printf("%d %d
", n, 1); // 单独判断是否存在大于sqrt(n)的质因子
    putchar('
');
}

int main(){
    scanf("%d", &t);
    while(t--){
        scanf("%d", &x);
        divide(x);
    }
    return 0;
}
---- suffer now and live the rest of your life as a champion ----
原文地址:https://www.cnblogs.com/popodynasty/p/14674858.html