牛客编程题(C语言):HJ6 质数因子

https://www.nowcoder.com/exam/oj/ta?tpId=37

https://www.nowcoder.com/practice/196534628ca6490ebce2e336b47b3607

提交代码

#include <stdio.h>
int main() {
    int n;
    scanf("%d", &n);
    for(int i=2; i*i<=n; i++) {
        while(n%i==0) {
            printf("%d ", i);
            n /= i;
        }
    }
    if(n>1) printf("%d ", n);
    return 0;
}

执行结果

注意

判断到\(\sqrt{n}\)即可停止,节省后一半时间,防止判断大数时超时

数学公式latex在线:https://www.latexlive.com/

posted @ 2022-05-15 18:05  孤舟浮岸  阅读(95)  评论(0编辑  收藏  举报