PAT_A1059#Prime Factors

Source:

PAT A1059 Prime Factors (25 分)

Description:

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

Keys:

  • 模拟题

Attention:

  • 注意N==1的情况

Code:

 1 /*
 2 Data: 2019-07-08 20:14:37
 3 Problem: PAT_A1059#Prime Factors
 4 AC: 14:40
 5 
 6 题目大意:
 7 打印正整数N的质因子,及其个数
 8 */
 9 #include<cstdio>
10 #include<vector>
11 #include<cmath>
12 using namespace std;
13 typedef long long LL;
14 struct node
15 {
16     LL p,k;
17 };
18 
19 int main()
20 {
21     LL n;
22     vector<node> fac;
23     scanf("%lld", &n);
24     printf("%lld=", n);
25     for(LL i=2; i<=(LL)sqrt(1.0*n); i++)
26     {
27         if(n%i == 0)
28         {
29             node t = node{i,0};
30             while(n%i==0 && n!=1)
31             {
32                 t.k++;
33                 n /= i;
34             }
35             fac.push_back(t);
36         }
37         if(n == 1)
38             break;
39     }
40     if(n != 1)
41         fac.push_back(node{n,1});
42     if(fac.size()==0)
43         printf("1");
44     for(int i=0; i<fac.size(); i++)
45     {
46         printf("%lld", fac[i].p);
47         if(fac[i].k != 1)
48             printf("^%lld", fac[i].k);
49         printf("%c", i==fac.size()-1?'\n':'*');
50     }
51 
52     return 0;
53 }

 

posted @ 2019-07-08 20:15  林東雨  阅读(170)  评论(0编辑  收藏  举报