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& ...
随机推荐
- 如何清除本机DNS缓存
如何清除本机DNS缓存 在实际应用过程中可能会遇到DNS解析错误的问题,就是说当我们访问一个域名时无法完成将其 解析到IP地址的工作,而直接输入网站IP却可以正常访问,这就是因为DNS解析出现故障造成 ...
- 《DSP using MATLAB》Problem 8.38
代码: function [wpLP, wsLP, alpha] = bp2lpfre(wpbp, wsbp) % Band-edge frequency conversion from bandpa ...
- 第十七篇:csv拆分、csv转excel方法
首先对微软的office功能表示敬佩!可能是这些办公软件太过平常化,所以体会不到他有多牛!csv格式数据以前没接触过,百度百科定义,Comma-Separated Values,CSV,逗号分隔值,或 ...
- SpringMVC的Hello World
本次使用Maven和Spring IO platform创建SpringMVC的Hello World. 一.Maven的Pom文件内容如下: <project xmlns="http ...
- LINUX挂接UNIX系统NFS文件共享
类似于windows的网络共享,UNIX(Linux)系统也有自己的网络共享,那就是NFS(网络文件系统),下面我们就以SUN Solaris2.8和REDHAT as server 3 为例简单介绍 ...
- thinkphp 虚拟模型
虚拟模型是指虽然是模型类,但并不会真正的操作数据库的模型.有些时候,我们建立模型类但又不需要进行数据库操作,仅仅是借助模型类来封装一些业务逻辑,那么可以借助虚拟模型来完成.虚拟模型不会自动连接数据库, ...
- 暑期集训日志(Day0~Day5)
章·五:2019-07-15:明月不谙离恨苦,斜光到晓穿朱户 ·昨日小结: 昨天考试又是爆零边缘,除了难过就剩难过了. T1暴力打崩了只拿了5分. T2没给分时间.最后20分钟打了个残码.没仔细观察数 ...
- SQLite C++操作类
为了方便SQLite的使用,封装了一个SQLite的C++类,同时支持ANSI 和UNICODE编码.代码如下: 头文件(SQLite.h) [cpp] view plaincopy /***** ...
- Go基础之函数递归实现汉诺塔
Go递归实现汉诺塔 package main import "fmt" // a 是源,b 借助, c 目的长度 func tower(a, b, c string, layer ...
- nginx 解决问题