文章

119

粉丝

68

获赞

92

访问

20.0k

头像
最大素因子 题解:素数筛选法,快速得到素因子
P1464 西安电子科技大学机试题
发布于2025年2月11日 13:23
阅读数 80

#include <bits/stdc++.h>

using namespace std;

int stringToInt(string s){
    int ans=0;
    for(auto x:s){
        if(isdigit(x)){
            ans*=10;
            ans+=x-'0';
        }
    }
    return ans;
}

int getMaxPrime(int x){
    int ans=2;
    for(int i=2;i*i<=x;i++){
        while(x%i==0){
            ans=i;
            x/=i;
        }
    }
    if(x!=1)ans=x;
    return ans;
}

int main(){
    int n;
    while(cin>>n){
        while(n--){
            string s;cin>>s;
            int x=stringToInt(s);
            if(x==0)cout<<"0"<<endl;
            else {
                cout<<getMaxPrime(x)<<endl;
            }
        }
    }
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发