⬅ 返回

质因数分解将每个整数表示为质数的唯一乘积,分解过程通常从最小的质数开始试除,直至商为质数。

1、试除法

#include <iostream>
 
using namespace std;
 
int main(){
    int n;
    cin>>n;
    while(n--)
    {
        int x;cin>>x;
        for(int i=2;i<=x/i;i++)
        {
            if(x%i==0)
            {
                int s = 0;
                while(x%i==0)
                {
                    s++;
                    x=x/i;
                }
                
                cout<<i<<" "<<s<<endl;
            }
        }
        if(x>1)cout<<x<<" "<<1<<endl;
        cout<<endl;
    }
    
}