【算法每日一练】PAT甲级 1059 java

1059 Prime Factors (25分)

Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p1kp2k2×⋯×pmk**m.

Input Specification:

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

Output Specification:

Factor N in the format N = p1^k1*p2^k2**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

代码

package com.zixin.algorithm;

import java.util.Scanner;

public class PATA1059 {

	static int prim[] = new int[100010];
	static int pNum = 0;

	public static void main(String[] args) {
		findPrim();//填充素数表
		int num = 0;// 不同质因数的个数
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		sc.close();
		factor fac[] = new factor[10];
		if (n == 1) {//1特殊处理
			System.out.println("1=1");
		} else {
			System.out.print(n + "=");
			int sqrt = (int) Math.sqrt(n * 1.0);
			for (int i = 0; i < pNum && prim[i] <= sqrt; i++) {//素数从前往后遍历 
				if (n % prim[i] == 0) {
					factor f = new factor();
					f.x = prim[i];
					f.cnt = 0;
					while (n % prim[i] == 0) {//如果可以相除的话 要判断可以处理几次
						f.cnt = f.cnt + 1;
						n /= prim[i];
					}
					fac[num++] = f;
				}
				if (n == 1) {
					break;
				}

			}
			if (n != 1) {
				factor f = new factor();
				f.x = n;
				f.cnt = 1;
				fac[num++] = f;
			}

			for (int i = 0; i < num; i++) {
				if (i > 0) {
					System.out.print("*");
				}
				System.out.print(fac[i].x);
				if (fac[i].cnt > 1) {
					System.out.print("^" + fac[i].cnt);
				}
			}
		}

	}

	static void findPrim() {
		for (int i = 1; i < 100010; i++) {
			if (isPrim(i)) {
				prim[pNum++] = i;
			}
		}
	}

	static boolean isPrim(int n) {
		if (n <= 1) {
			return false;
		}
		int seq = (int) Math.sqrt(n * 1.0);
		for (int i = 2; i <= seq; i++) {
			if (n % i == 0) {
				return false;
			}
		}
		return true;
	}
}

class factor {
	int x;// 素数
	int cnt;// 个数
}

提交

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值