题目描述:
题目描述
对于给定的字符序列,从左至右将所有的数字字符取出拼接成一个无符号整数(字符序列长度小于100,拼接出的整数小于2^31,),计算并输出该整数的最大因子(如果是素数,则其最大因子为自身)
输入
有多组数据,输入数据的第一行为一个正整数,表示字符序列的数目,每组数据为一行字符序列。
输出
对每个字符序列,取出所得整数的最大因子,若字符序列中没有数字或者找出的整数为0,则输出0,每个整数占一行输出。
样例输入
3
sdf0ejg3.f?9f
?4afd0s&2d79*(g
abcde
样例输出
13
857
0
这一题由于题目没叙述清楚,估计后台数据是让求最大质因子,所以这一题郁闷了一天,终于AC了,没有啥算法,其实就是利用了质因子分解的性质,取最大就行了。
AC代码
#include <stdio.h>
#include <ctype.h>
int main()
{
char s[105];
int n;
while(~scanf("%d", &n))
{
int cnt = n;
int temp, max;
while(cnt --)
{
temp = max = 0;
scanf("%s", s);
for(int i = 0;s[i];i ++)
{
if(isdigit(s[i]))
temp = temp * 10 + s[i] - '0';
}
if(temp == 0 || temp == 1)
{
printf("%d\n", temp);
continue;
}
for(int i = 2;i * i <= temp;i ++)
{
if(temp % i == 0)
{
max = i;
}
while(temp % i == 0)
{
temp /= i;
}
}
if(max < temp)
max = temp;
printf("%d\n",max);
}
}
return 0;
}
题目传送门