采用递归思路获取 最大质数因子
#include<iostream>
#include<math.h>
#include<algorithm>
#include<string>
#include<iomanip>
#define MAX 128
using namespace std;
//获取数字
string get_number(string ss) {
string res;
for (int i = 0; i < ss.size(); i++) {
if (ss[i] >= '0'&&ss[i] <= '9') {
res += ss[i];
}
}
return res;
}
//获取最大素数因子
int get_prime_factor(int a) {
//cout << "执行" << endl;
if (a == 1 || a == 2)return a;
for (int i = 2; i <= sqrt(a); i++) {
if (a % i == 0) {
return max(get_prime_factor(a/i), i);
}
}
return a;
}
int main() {
//cout << get_prime_factor(28) << endl;
int n; cin >> n;
for (int i = 0; i < n; i++) {
string ss; cin >> ss;
string s = get_number(ss);
if (s.size() == 0) {
cout << 0 << endl;
}
else {
int a = stoi(s);
int pFactor = get_prime_factor(a);
cout << pFactor << endl;
}
}
return 0;
}