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 = 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^2*11*17*101*1291
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <iostream>
 4 #include <string.h>
 5 #include <string>
 6 #include <math.h>
 7 #include <algorithm>
 8 using namespace std;
 9
10
11 const int maxn=100000;
12 int prime[maxn],pnum=0;
13 bool p[maxn]={0};
14 void Find_Prime(){
15     for(int i=2;i<maxn;i++)
16     {
17         if(p[i]==false)
18         {
19             prime[pnum++]=i;
20             for(int j=i+i;j<maxn;j+=i)
21             {
22                 p[j]=true;
23             }
24         }
25     }
26 }
27
28 struct factor{
29     int x,cnt;
30 }fac[10];
31 int main(){
32    Find_Prime();
33    int n,num=0;
34    scanf("%d",&n);
35    if(n==1){
36    printf("1=1");
37    return 0;
38    }else{
39        printf("%d=",n);
40    }
41
42
43    int sqr=(int)sqrt(1.0*n);
44    int count=0;
45    for(int i=2;i<=sqr;)
46    {
47        if(n%i==0)
48        {
49            fac[num].x=i;
50            fac[num].cnt=0;
51            while(n%i==0)
52            {
53                fac[num].cnt++;
54                n/=i;
55            }
56            num++;
57        }
58
59        count++;
60        i=prime[count];
61    }
62    if(n>sqr)
63    {
64        fac[num].x=n;
65        fac[num++].cnt=1;
66    }
67    for(int i=0;i<num;i++)
68    {
69        if(i>0)printf("*");
70        printf("%d",fac[i].x);
71        if(fac[i].cnt>1)
72        {
73            printf("^%d",fac[i].cnt);
74        }
75    }
76     return 0;
77 }
时间: 2024-08-11 13:23:58

A1059. Prime Factors (25)的相关文章

PAT 1059. Prime Factors (25)

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 inte

pat1059. Prime Factors (25)

1059. Prime Factors (25) 时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HE, Qinming 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 Specificatio

1059. Prime Factors (25)

时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HE, Qinming 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 contain

PAT:1059. Prime Factors (25) AC

#include<stdio.h> #include<math.h> #include<string.h> #include<algorithm> using namespace std; const int MAX=100010; //int型素数一定在这个范围内 int PrimeArr[MAX]; //素数表 int cnt=0; //素数表中素数个数 int x,x2; //输入的数字,x2是x的开平方数,x的因子只有从2到根号x+1 struct

PAT1059. Prime Factors

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 lon

[CareerCup] 7.7 The Number with Only Prime Factors 只有质数因子的数字

7.7 Design an algorithm to find the kth number such that the only prime factors are 3,5, and 7. 这道题跟之前LeetCode的那道Ugly Number II 丑陋数之二基本没有啥区别,具体讲解可参见那篇,代码如下: class Solution { public: int getKthMagicNumber(int k) { vector<int> res(1, 1); int i3 = 0, i

2014辽宁ACM省赛 Prime Factors

问题 L: Prime Factors 时间限制: 1 Sec  内存限制: 128 MB 提交: 36  解决: 28 [提交][状态][论坛] 题目描述 I'll give you a number , please tell me how many different prime factors in this number. 输入 There is multiple test cases , in each test case there is only one line contain

1059 Prime Factors

题意: 给出一个int型正整数N,要求把N分解成若干个质因子,如N=97532468,则把N分解成:97532468=2^2*11*17*101*1291.质因子按增序输出,如果某个质因子的幂是1,则1不输出. 思路:质因子分解的基础题. 首先,定义如下因子的结构体,用于存放最终的结果.因为N是一个int范围的正整数,由于2*3*5*7*11*13*17*19*23*29>INT_MAX,也就是说,任意一个int型整数,可分解出来的不同的质因子的个数不会超过10个,因此,数组只要开到10就够了.

patA1059 Prime Factors

这个问题叫做质因子分解,花了大概两个小时写对了.这道题细节挺多的,书上提到了几点,有一个很容易错的点就是n一开始要先用一个变量保存起来,不保存的话后面有点麻烦,所以建议还是先保存起来.因为过程中要不断的改变n,最后还要打印出n,如果n为1的话还要特殊处理.当然也可以一开始处理打印,不过我觉得和思维颠倒有些难受.一个很重要的问题就是打印的素数表需要多大,这个我也不清楚怎么办,所以先写出来看,随机应变,如果不能完全覆盖的话再往上加,最后完全覆盖了那个样例.最后解决的细节是当我输入2的时候一开始只探测