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 = 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^2*11*17*101*1291

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include <climits>
 3 #include<iostream>
 4 #include<vector>
 5 #include<queue>
 6 #include<map>
 7 #include<set>
 8 #include<stack>
 9 #include<algorithm>
10 #include<string>
11 #include<cmath>
12 using namespace std;
13 bool Prime[100000];
14 void Make_Prime(int N)
15 {
16     fill(Prime, Prime + N+1, true);
17     for (int i = 2; i<N;i++)
18     {
19         if (!Prime[i])continue;
20         for (int j = 2; i * j <N; j++)
21             Prime[i * j] = false;
22     }
23 }
24 
25 int main()
26 {
27     Make_Prime(100000);
28     long long N;
29     cin >> N;
30     cout << N << "=";
31     if (N == 1)
32         cout << N;
33     bool have = false;
34     for (int i = 2; i < N; i++)
35     {
36         bool flag = false;
37         int count = 0;
38         while(Prime[i] && N % i == 0)
39         {
40             flag = true;
41             count++;
42             N /= i;
43         }
44         if (flag)
45         {
46             if (have)cout << "*";
47             cout << i;
48             if (count > 1)
49                 cout << "^" << count;
50             have = true;
51         }
52     }
53     if (N > 1)
54         if (have)cout << "*" << N;
55         else cout << N;
56 }
View Code


原文地址:https://www.cnblogs.com/57one/p/12054818.html