【剑指紫金港】1059 Prime Factors

A 1059 Prime Factors

题目链接

Problem Description

Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p​1​​ ​k​1​​ ​​ ×p​2​​ ​k​2​​ ​​ ×⋯×p​m ​k​m​​ ​​ .

Input

Each input file contains one test case which gives a positive integer N in the range of long int.

Output

Factor N in the format N = p​1​​ ^k1​​ *p​2​​ ^k​2​​ *…*p​m​​ ^k​m
​​ , where p​i​​ 's are prime factors of N in increasing order, and the exponent k​i​​ is the number of p​i​​ – hence when there is only one p​i​​ , k​i​​ is 1 and must NOT be printed out.

Sample Input:

97532468

Sample Output:

97532468=2^2*11*17*101*1291

题目大意

质因数分解

解题思路

先找出10000以内的所有质数,然后用n不断去÷这些质数,分解到最后如果n大于1,说明n有一个大于10000的质因数,再输出这个质因数。特判n=1的情况

AC代码

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxn = 10005;
ll prime[maxn],n;
int num[maxn],ind=0,cnt=0;
bool isprime(ll x){
	if(x<=1) return false;
	const int len = (int)sqrt(1.0*x);
	for(int i=2;i<=len;i++){
		if(x%i==0) return false;
	}
	return true;
}
void getprime(){
	for(ll i=2;i<10000;i++){
		if(isprime(i)){
			prime[ind++]=i;
		}
	}
}
int main(){
	getprime();
	memset(num,0,sizeof(num));
	scanf("%lld",&n);
	printf("%lld=",n);
    if(n==1){
        printf("1");
        return 0;
    }
	for(int i=0;i<ind;i++){
		ll temp=prime[i];
		while(n>1){
			if(n%temp==0){
				num[i]++;
				n/=temp;
			}else{
				break;
			}
		}
	}
	for(int i=0;i<ind;i++){
		if(num[i]>0){
			if(cnt>0) printf("*");
			printf("%lld",prime[i]);
			if(num[i]>1) printf("^%d",num[i]);
			cnt++;
		}
	}
	if(n>1){
		printf("*%lld",n);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

征服所有不服

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值