Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p​1​​​k​1​​​​×p​2​​​k​2​​​​×⋯×p​m​​​k​m​​​​.

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 = p​1​​^k​1​​*p​2​​^k​2​​**p​m​​^k​m​​, where p​i​​'s are prime factors of N in increasing order, and the exponent k​i​​ is the number of p​i​​ -- hence when there is only one p​i​​, k​i​​ is 1 and must NOT be printed out.

Sample Input:

97532468

Sample Output:

97532468=2^2*11*17*101*1291
 #include <iostream>
#include <cmath>
const int maxn = ;
bool isprime(int n)
{//判断n是否为素数
if (n == )
return false;
int sqr = (int)sqrt(1.0*n);
for (int i = ; i <= sqr; i++)
{
if (n % i == )
return false;
}
return true;
}
int prime[maxn], pNum = ;
void FindPrime()
{//求素数表
for (int i = ; i < maxn; i++)
{
if (isprime(i) == true)
prime[pNum++] = i;
}
}
struct factor
{
int x,cnt;//x为质因子,cnt为其个数
}fac[]; int main()
{
FindPrime();//此句必须记得写
int n, num = ;//num为n的不同质因子的个数
scanf("%d", &n);
if (n == )
printf("1=1");//特判1的情况
else
{
printf("%d=", n);
int sqr = (int)sqrt(1.0*n);//n的根号
//枚举根号n以内的质因子
for (int i = ; i < pNum&&prime[i] <= sqr; i++)
{
if (n%prime[i] == )//如果prime[i]是n的因子
{
fac[num].x = prime[i];//记录该因子
fac[num].cnt = ;
while (n%prime[i] == )
{//计算出质因子prime[i]的个数
fac[num].cnt++;
n /= prime[i];
}
num++;//不同质因子个数加1
}
if (n == )
break;//及时退出循环,节省点时间
}
if (n != )
{//如果无法被根号n以内的质因子除尽
fac[num].x = n;//那么一定有一个大于根号n的质因子
fac[num++].cnt = ;
}
//按格式输出结果
for (int i = ; i < num; i++)
{
if (i > )
printf("*");
printf("%d", fac[i].x);
if (fac[i].cnt > )
printf("^%d", fac[i].cnt);
}
}
return ;
}

PAT甲级——A1059 Prime Factors的更多相关文章

  1. PAT 甲级 1059 Prime Factors

    https://pintia.cn/problem-sets/994805342720868352/problems/994805415005503488 Given any positive int ...

  2. PAT 甲级 1059 Prime Factors (25 分) ((新学)快速质因数分解,注意1=1)

    1059 Prime Factors (25 分)   Given any positive integer N, you are supposed to find all of its prime ...

  3. A1059. Prime Factors

    Given any positive integer N, you are supposed to find all of its prime factors, and write them in t ...

  4. PAT Advanced 1059 Prime Factors (25) [素数表的建⽴]

    题目 Given any positive integer N, you are supposed to find all of its prime factors, and write them i ...

  5. PAT 甲级 1096 Consecutive Factors

    https://pintia.cn/problem-sets/994805342720868352/problems/994805370650738688 Among all the factors ...

  6. PAT甲级——1096 Consecutive Factors (数学题)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/91349859 1096 Consecutive Factors  ...

  7. PAT甲级——A1096 Consecutive Factors【20】

    Among all the factors of a positive integer N, there may exist several consecutive numbers. For exam ...

  8. PAT_A1059#Prime Factors

    Source: PAT A1059 Prime Factors (25 分) Description: Given any positive integer N, you are supposed t ...

  9. PAT甲级题解分类byZlc

    专题一  字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...

随机推荐

  1. Android NDK应用原理

    转:http://shihongzhi.com/ndk/ 那么首先看一下Android的系统框架: 最底层是Linux Kernel,然后上面是封装的库及Android runtime.再上面是App ...

  2. Vue-Grid-Layout分享一款好用的可拖拽组件

    在使用Grafana的过程中,发现Grafana关于视图页面中每一个面板都可拖拽,可随意放大放小,体验非常棒,F12看了Grafana的代码,看打包后的代码很像react,进一步css,看到有grid ...

  3. 14. TIM of STM32F103ZE

    block diagram 14.3.1 Time-base unit 有三个基础的寄存器: 计数寄存器(TIMx_CNT) 预分配寄存器(TIMx_PSC), 自动重载寄存器(TIMx_ARR) 重 ...

  4. Java学习之垃圾回收机制

    垃圾回收机制,依赖JRE和JVM,涉及操作系统中内存的分配与回收.依据所学,我猜想这种机制需要的数据结构是堆内存分配表(链),管理已分配和未分配的堆内存,对于已分配堆内存,需要知道由栈内存中的哪些变量 ...

  5. 08_springboot2.x自定义starter

    概述 starter:启动器 1.这个场景需要使用到的依赖是什么? 2.如何编写自动配置 规则: @Configuration //指定这个类是一个配置类 @ConditionalOnXXX //在指 ...

  6. Django框架中session存储到redis中的配置

    本文链接:https://blog.csdn.net/linqunbin/article/details/94786313————————————————版权声明:本文为CSDN博主「linqunbi ...

  7. WPF 实现 TextBox 只能输入数字并且不能使用拷贝功能

    1.代码页需要在键盘按下事件中对输入文字进行筛选,代码如下: private void tbxGoToPage_PreviewKeyDown(object sender, KeyEventArgs e ...

  8. Vim ---- 默认打开行号

    Vim有非常迅速跳转到某一行行首的方法,例如 :n 或者 nG,n 表示到第 n 行. 但是Vim的显示行号功能默认是关闭的. 可用一下方法使Vim默认显示行号: 在配置文件 .vimrc 中,输入 ...

  9. Ubuntu clion下载及激活

    1.下载 方法:去官网下载clion  https://www.jetbrains.com/clion/download/#section=linux 或者使用我上传的百度网盘链接: https:// ...

  10. Ionic3 demo TallyBook 实例2

    1.添加插件 2.相关页面 消费页面: <ion-header> <ion-navbar> <ion-title> 消费记录 </ion-title> ...