Codeup墓地-1980

Codeup墓地-1980
src:http://codeup.cn/problem.php?id=1980
1980: Problem C
时间限制: 1 Sec 内存限制: 32 MB
题目描述
对于给定的字符序列,从左至右将所有的数字字符取出拼接成一个无符号整数(字符序列长度小于100,拼接出的整数小于2^31,),计算并输出该整数的最大素因子(如果是素数,则其最大因子为自身)
输入
有多组数据,输入数据的第一行为一个正整数,表示字符序列的数目,每组数据为一行字符序列。

输出
对每个字符序列,取出所得整数的最大素因子,若字符序列中没有数字或者找出的整数为0,则输出0,每个整数占一行输出。

样例输入
3
sdf0ejg3.f?9f
?4afd0s&2d79*(g
abcde
样例输出
13
857
0

//求一个数的最大素因子
#include   
#include   
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;  
}

你可能感兴趣的:(codeup墓地)