PAT:A1059 Prime Factors (25 分)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Ecloss/article/details/82664554

PAT:A1059 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 = p​1​​​k​1​​​​×p​2​​​k​2​​​​×⋯×p​m​​​k​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 = p​1​​^k​1​​*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

代码:

#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;

struct factor{
	int x, cut;		// x代表质因子, cut代表质因子的个数 
}f[11];

// 因为给出的是int范围内的整数
// 判断是否是素数
bool isPrimare(int n) {
	if(n <= 1) return false;
	int sqr = (int)sqrt(1.0 * n);
	for(int i = 2; i <= sqr; i++) {
		if(n % i == 0) return false;
	}
	return true;
} 

// 求出素数表
const int maxn = 100010;
int p[maxn] = {0}, pNum = 0;
void FindPrimare() {
	for(int i = 1; i < maxn; i++) {
		if(isPrimare(i) == true) {
			p[pNum++] = i;
		}
	}
} 

int main() {
	FindPrimare();
	int n, num = 0;
	scanf("%d", &n);
	if(n == 1) printf("1=1");
	else {
		printf("%d=", n); 
		int q = (int)sqrt(1.0 * n);
		for(int i = 0; i < pNum && p[i] <= q; i++) {
			if(n % p[i] == 0) {
				f[num].x = p[i];
				f[num].cut = 0;
				while(n % p[i] == 0) {
					f[num].cut++;
					n = n / p[i];
				}
				num++;
			}
			if(n == 1) break;
		}
		if(n != 1) {
			f[num].x = n;
			f[num].cut = 1;
			num++;
		}
		for(int i = 0; i < num; i++) {
			printf("%d", f[i].x);
			if(f[i].cut > 1) printf("^%d", f[i].cut);
			if(i < num-1) printf("*");
		}
	}
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Ecloss/article/details/82664554