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 = p1 k1 ×p2 k2 ×⋯×pm km
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 *…*pm ^km , where pi 's are prime factors of N in increasing order, and the exponent ki is the number of pi – hence when there is only one pi , ki is 1 and must NOT be printed out.
Sample Input:
97532468
Sample Output:
97532468=2^211171011291
#include<bits/stdc++.h>
using namespace std;
struct Node {
int x, cnt;
}node[10];
int Prime[100000], pnum = 0, num = 0, N, sqr, n;
bool judge[200000] = {false};
void init(int n) {
for (int i = 2; i <= n; i++) {
if (judge[i] == false) {
Prime[pnum++] = i;
}
for (int j = i + i; j <= n; j += i) {
judge[j]= true;
}
}
}
int main() {
scanf("%d", &N);
sqr = sqrt(1.0 * N);
init(sqr);
n = N;
for (int i = 0; i < pnum; i++) {
if (Prime[i] > sqr) break;
if (N % Prime[i] == 0) {
node[num].x = Prime[i];
node[num].cnt = 0;
while (N % Prime[i] == 0) {
node[num].cnt++;
N /= Prime[i];
}
num++;
}
}
if (num == 0) {
node[num].cnt = 1;
node[num++].x = n;
}
printf("%d=", n);
for (int i = 0 ; i < num; i++) {
if (i != 0) printf("*");
printf("%d", node[i].x);
if (node[i].cnt > 1) printf("^%d", node[i].cnt);
}
}