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& ...
随机推荐
- 尚学linux课程---6、linux命令介绍
尚学linux课程---6.linux命令介绍 一.总结 一句话总结: linux中命令的一般格式:命令关键字 选项 参数1 参数2 1.linux基本原则? 一切皆文件 配置文件保存为纯文本格式 2 ...
- namespace 命名空间
namespace作用:资源隔离 当我们不指定namespace时,默认放在default下 创建namespace kubectl create namespace 资源名称 在生产中,我们建议一个 ...
- Spring 基于Aspectj切面表达式(6)
1 package com.proc; 2 3 import org.aspectj.lang.JoinPoint; 4 import org.aspectj.lang.ProceedingJoinP ...
- 半宿了,仿写了个CList模板类,留着以后用吧
难题还是很多阿.模板类.本身就是个问题 要考虑到各个方面,哎.问题很多 比如,如果模板类型为数值型.指针型数据,倒也可以 但是如果模板类型为聚合型,就麻烦了,判断.比较,什么乱七八糟的,都麻烦了. 哎 ...
- java_网络编程之BS(web案例)
package BsServersocket; import java.io.*; import java.net.ServerSocket; import java.net.Socket; publ ...
- 设置Hadoop+Hbase集群pid文件存储位置
有时候,我们对运行几天或者几个月的hadoop或者hbase集群做停止操作,会发现,停止命令不管用了,为什么呢? 因为基于java开发的程序,想要停止程序,必须通过进程pid来确定,而hadoop和h ...
- 属性面板:tabcontroller
Tabcontroller 布局 Anchor 设置控件距离选定方向固定: Dock 定义要绑定到容器的控件边框 Location 设置控件对于容器左上角的坐标 Margin 指定此控件与另一控件边距 ...
- 洛谷P2325 [SCOI2005]王室联邦
P2325 [SCOI2005]王室联邦 题目描述 "余"人国的国王想重新编制他的国家.他想把他的国家划分成若干个省,每个省都由他们王室联邦的一个成员来管理. 他的国家有n个城市, ...
- CodeForces 258D Little Elephant and Broken Sorting(期望)
CF258D Little Elephant and Broken Sorting 题意 题意翻译 有一个\(1\sim n\)的排列,会进行\(m\)次操作,操作为交换\(a,b\).每次操作都有\ ...
- LoadRunner内部结构(1)
LoadRunner内部结构(1) 根据http://www.wilsonmar.com/1loadrun.htm 翻译: LoadRunner内部结构 1, 被测系统是由驱动 ...