【PAT甲级 - C++题解】1059 Prime Factors

简介: 【PAT甲级 - C++题解】1059 Prime Factors

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


题意

给定一个整数 N NN ,找出它的所有质因子,并按如下格式输出:

N=p1k1×p2k2×...×pmkm


注意: 如果 N = 1 N=1N=1 则输出 1=1。


思路

我们可以直接从 2 22 往后遍历所有质数,然后计算是否是 n nn 的质因子。因为遍历是从小到大进行的,所以可以从 2 22 遍历到 n nn ,尽管遍历的不是质数,由于前面小的质数已经计算完,所以后面大的不是质数的数 n nn 一定不会整除它。


但是由于给定的数据范围比较大,直接遍历到 n nn 可能会超时,所以可以只遍历其所有约数,效果也是一样的,即从 2 22 遍历到 n \sqrt{n} n  。


代码

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin >> n;
    printf("%d=", n);
    if (n == 1)    puts("1");
    else
    {
        bool first = true;
        //遍历质数
        for (int i = 2; i <= n / i; i++)
            if (n % i == 0)  //判断是否能除尽
            {
                //计算能除尽该质因子的几次幂
                int k = 0;
                while (n % i == 0)   n /= i, k++;
                if (!first)  cout << "*";
                else    first = false;
                cout << i;
                if (k > 1) cout << "^" << k;
            }
        //如果n已经除不尽且大于1,也要输出到后面
        if (n > 1)
        {
            if (!first)  cout << "*";
            cout << n;
        }
    }
    return 0;
}


目录
相关文章
|
C++
【PAT甲级 - C++题解】1040 Longest Symmetric String
【PAT甲级 - C++题解】1040 Longest Symmetric String
201 0
|
算法 C++
【PAT甲级 - C++题解】1044 Shopping in Mars
【PAT甲级 - C++题解】1044 Shopping in Mars
245 0
|
C++
【PAT甲级 - C++题解】1117 Eddington Number
【PAT甲级 - C++题解】1117 Eddington Number
244 0
|
存储 C++ 容器
【PAT甲级 - C++题解】1057 Stack
【PAT甲级 - C++题解】1057 Stack
230 0
|
存储 C++
【PAT甲级 - C++题解】1055 The World‘s Richest
【PAT甲级 - C++题解】1055 The World‘s Richest
146 0
|
C++
【PAT甲级 - C++题解】1051 Pop Sequence
【PAT甲级 - C++题解】1051 Pop Sequence
166 0
|
人工智能 BI C++
【PAT甲级 - C++题解】1148 Werewolf - Simple Version
【PAT甲级 - C++题解】1148 Werewolf - Simple Version
335 0
|
12月前
|
编译器 C++ 开发者
【C++篇】深度解析类与对象(下)
在上一篇博客中,我们学习了C++的基础类与对象概念,包括类的定义、对象的使用和构造函数的作用。在这一篇,我们将深入探讨C++类的一些重要特性,如构造函数的高级用法、类型转换、static成员、友元、内部类、匿名对象,以及对象拷贝优化等。这些内容可以帮助你更好地理解和应用面向对象编程的核心理念,提升代码的健壮性、灵活性和可维护性。
|
10月前
|
编译器 C++ 容器
【c++11】c++11新特性(上)(列表初始化、右值引用和移动语义、类的新默认成员函数、lambda表达式)
C++11为C++带来了革命性变化,引入了列表初始化、右值引用、移动语义、类的新默认成员函数和lambda表达式等特性。列表初始化统一了对象初始化方式,initializer_list简化了容器多元素初始化;右值引用和移动语义优化了资源管理,减少拷贝开销;类新增移动构造和移动赋值函数提升性能;lambda表达式提供匿名函数对象,增强代码简洁性和灵活性。这些特性共同推动了现代C++编程的发展,提升了开发效率与程序性能。
394 12
|
8月前
|
人工智能 机器人 编译器
c++模板初阶----函数模板与类模板
class 类模板名private://类内成员声明class Apublic:A(T val):a(val){}private:T a;return 0;运行结果:注意:类模板中的成员函数若是放在类外定义时,需要加模板参数列表。return 0;
210 0