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 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

以下算法可以改进为比3大的可以直接加2,忽略偶数
 1 #include <iostream>
 2 #include <vector>
 3
 4 using namespace std;
 5
 6 int main()
 7 {
 8     vector<int> factors, radix;
 9     long factor = 2;
10     long num;
11     cin >> num;
12     cout << num << "=";
13     if (num == 1)
14     {
15         cout << 1;
16         return 0;
17     }
18     while (num != 1)
19     {
20         int cnt = 0;
21         if (num % factor == 0)
22             factors.push_back(factor);
23         while (num%factor == 0)
24         {
25             cnt++;
26             num /= factor;
27         }
28         if (cnt!=0)
29             radix.push_back(cnt);
30         factor++;
31     }
32
33     for (int i = 0; i < factors.size() - 1; i++)
34     {
35         cout << factors[i];
36         if (radix[i]>1)
37             cout << "^" << radix[i];
38         cout << "*";
39     }
40     cout << factors.back();
41     if (radix.back() > 1)
42         cout << "^" << radix.back();
43 }
时间: 2024-10-19 06:16:41

PAT 1059. 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 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

PAT 1059. Prime Factors

反正知道了就是知道,不知道也想不到,很快 #include <cstdio> #include <cstdlib> #include <vector> using namespace std; inline void print_prime_k(long long p, long long k) { printf("%lld", p); if (k > 1) { printf("^%lld", k); } } int mai

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

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 lon

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就够了.

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