Codevs 1792 分解质因数

来源:互联网 发布:linux系统品牌 编辑:程序博客网 时间:2024/06/02 13:08

不知道还有没有更优的算法……虽然我的能过……
求教QAQ。

代码:

#include <iostream>#include <cstdio>#include <cstring>#include <queue>#include <cmath>#include <ctime>#define X CLOCKS_PER_SECusing namespace std;typedef long long LL;bool Judge(LL x){    if(x <= 1)        return false;    if(x <= 3)        return true;    for(LL i = 2; i <= sqrt(x); i ++)        if(x%i == 0)            return false;    return true;}int main(){    LL n;    cin >> n;    printf("%lld=", n);    if(Judge(n))    {        printf("%lld\n",n);        return 0;    }    bool first = 1;    LL i = 2;    while(n && n != 1)    {        if(!Judge(i))        {            i ++;            continue;        }        LL cnt = 0;        while(n%i == 0)         {            cnt ++;            n/= i;        }        while(cnt --)        {            if(!first)                printf("*%lld", i);            else {                printf("%lld", i);                first = 0;            }        }        i ++;    }    return 0;}// 1000000007
0 0
原创粉丝点击