2019-03-10 因子分解

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
using namespace std;
int h[1000];
int a[1000];
int cnt;
bool match(int p)
{
    for(int i=p;i<cnt;i++) if(a[i]) return true;
    return false; 
}
bool isPrime(int x)
{
    if(x<=2) return x==2;
    for(int i=2;i<=(int)sqrt(x)+1;i++) if(x%i==0) return false;
    return true; 
}
int main(void)
{
    int n=0,p=0;
    for(int i=2;i<=200;i++) if(isPrime(i)) h[cnt++]=i;
    scanf("%d",&n);
    while(n!=1)
    {
        while(n%h[p]==0 && n!=1)
        {
            a[p]++;
            n/=h[p]; 
        }
        p++;
    }
    for(int i=0;i<cnt;i++)
    {
        if(a[i]) printf("%d",h[i]);
        if(a[i]!=1 && a[i]!=0) printf("^%d",a[i]);
        if(a[i]!=0 && match(i+1)) printf("*"); 
    }
    return 0;   
} 
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容