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 <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;  
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值