题解 | Problem C

Problem C

https://www.nowcoder.com/practice/2a05dcaa4cde4db989443f206ee3e5c5

#include <iostream>
#include<cmath>
using namespace std;


int factor(int num) {
    if (num <= 2)return num;
    int max_factor = -1;
    while(num%2==0){
        max_factor=2;
        num=num/2;
    }
    for (int i = 3; i <= sqrt(num); i = i + 2) {
        while (num % i == 0 ) {
            max_factor=i;
            num=num/i;
        }
    }
    if(num>max_factor)max_factor=num;
    return max_factor;
}

int main() {
    int a, b;
    while (cin >> a) { // 注意 while 处理多个 case
        string s;
        cin.ignore();
        for (int i = 0; i < a; i++) {
            b = 0;
            
            getline(cin, s);
            for (int i = 0; i < s.length(); i++) {
                if (s[i] >= '0' && s[i] <= '9')b = b * 10 + s[i] - '0';
            }
            // cout<<b<<endl;
            if (b == 0)cout << 0 << endl;
            else cout << factor(b) << endl;
        }
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

06-04 20:17
门头沟学院 Java
牛客713608542号:有的,我今天刚面了一个小厂,他们说刚好有缺人,就放出来了,成都的旅鸽,hxd不如去试试,但是是线下哇,不知道他们支不支持线上,如果有面记得多复习一下sql,我死在这一块上了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务