PAT甲级——A1059 Prime Factors
Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p1k1×p2k2×⋯×pmkm.
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
#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的更多相关文章
- PAT 甲级 1059 Prime Factors
https://pintia.cn/problem-sets/994805342720868352/problems/994805415005503488 Given any positive int ...
- 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 ...
- A1059. Prime Factors
Given any positive integer N, you are supposed to find all of its prime factors, and write them in t ...
- 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 ...
- PAT 甲级 1096 Consecutive Factors
https://pintia.cn/problem-sets/994805342720868352/problems/994805370650738688 Among all the factors ...
- PAT甲级——1096 Consecutive Factors (数学题)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/91349859 1096 Consecutive Factors ...
- PAT甲级——A1096 Consecutive Factors【20】
Among all the factors of a positive integer N, there may exist several consecutive numbers. For exam ...
- PAT_A1059#Prime Factors
Source: PAT A1059 Prime Factors (25 分) Description: Given any positive integer N, you are supposed t ...
- PAT甲级题解分类byZlc
专题一 字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...
随机推荐
- 按指定规则统计list中数据,groupby用法
有的情况下,只是想要简单地对list中数据,进行分组,查看,可以考虑使用groupby 代码: # groupby需要排序后才能使用 def gb(num): if 0 <= num < ...
- 面试系列10 es生产集群的部署架构
如果你确实干过es,那你肯定了解你们生产es集群的实际情况,部署了几台机器?有多少个索引?每个索引有多大数据量?每个索引给了多少个分片?你肯定知道! 但是如果你确实没干过,也别虚,我给你说一个基本的版 ...
- CSS三大特性之继承性
1.并不是所有的属性都可以继承,只有以color/font/text/line开头的属性 才可以继承. 2.在CSS的继承中,不仅仅是儿子可以继承,只要是后代都可以继承. 3.继承中的特殊性 3.1 ...
- 如何在安装了Owin 2.X版本的项目中正确安装SignalR 2.2.1
以安装了Owin2.1.0版本为例 1打开NuGet程序包管理控制台 2输入 Install-Package Microsoft.AspNet.SignalR -Version 2.2.1 2输入 I ...
- 02.MyBatis在DAO层开发使用的Mapper动态代理方式
在实际开发中,Mybatis作用于DAO层,那么Service层该如何调用Mybatis Mybatis鼓励使用Mapper动态代理的方式 Mapper接口开发方法只需要程序员编写Mapper接口(相 ...
- C++:多线程001
C++ 多线程 创建线程的API函数 HANDLE CreateThread( LPSECURITY_ATTRIBUTES lpThreadAttributes,//SD:线程安全相关的属性,常置为N ...
- Excel skill: 如何替换换行符,以及如何把一格转换成多行/多列
http://blog.sciencenet.cn/blog-508298-695290.html 增加一辅助列,用替换函数替换掉软回车.比如A列是数据,从A1开始,则插入B列,B1输入公式=REPL ...
- JavaScript设置body高度为浏览器高度的方法
document.getElementsByTagName('body')[0].style.height = window.innerHeight+'px';
- kafka数据分区的四种策略
kafka的数据的分区 探究的是kafka的数据生产出来之后究竟落到了哪一个分区里面去了 第一种分区策略:给定了分区号,直接将数据发送到指定的分区里面去 第二种分区策略:没有给定分区号,给定数据的ke ...
- 杂项-公司:Google
ylbtech-杂项-公司:Google 谷歌公司(Google Inc.)成立于1998年9月4日,由拉里·佩奇和谢尔盖·布林共同创建,被公认为全球最大的搜索引擎公司.谷歌是一家位于美国的跨国科技企 ...