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& ...
随机推荐
- 记录一次idea因为修改子模块名称而引申的一大堆问题(未完全解决)
文章目录 背景 看图说话 解决 因为这个案例引申出来的错误 修改了之后莫名出现在java 和resource文件后面出现sources root字样 修改了之后java和resource后面出现了[c ...
- ArrayList 和linkedList 插入比较
从学Java开始, 就一直大脑记着 arrayList 底层是数组 ,查询快, 插入慢, 有移动的动作.linkedList 底层链表, 插入快 查询慢,今天写了例子跑了跑, 果然. public ...
- Java程序员必备的10个大数据框架!
作者:java妞妞 blog.csdn.net/javaniuniu/article/details/71250316 当今IT开发人员面对的最大挑战就是复杂性,硬件越来越复杂,OS越来越复杂,编程语 ...
- Requests+Excel接口自动化测试(Python)
一.框架结构: 工程目录 二.Case文件设计 三.基础包 base 3.1 封装get/post请求(runmethon.py) import requests import json class ...
- Java开发系列-电子邮箱
概述 电子邮箱就是在邮箱服务器上开启的一块空间.邮箱服务器就是一个安装在计算机的服务软件,提供有邮件服务. 邮箱的发送流程 现在tom要发送一份邮件给jerry,首先tom将邮件通过客户端连接自己设置 ...
- node vue 微信公众号(四)配置环境 本地测试
1.去natap 配置端口号 //本地项目是8080端口,natapp就配置8080端口 2.ngrok配合vue-cli实现外网访问 1.去 https://ngrok.com/download 下 ...
- 【JZOJ3337】wyl8899的TLE
description wyl8899今天也很刻苦的在做老师布置下来的题目! 这一天老师布置的题目是这样的: 给出两个仅含小写字母的字符串A和B,输出最大的k,使得A[1..k]是B的子串. A和B的 ...
- Java可变参数与Collections工具类使用了解
今天发现jdk1.5后增加了个可变参数,以前还一直不晓得 public static void main(String[] args) { System.out.println(getNum(1,2, ...
- Flask从入门到入土
一.flask介绍 Flask是一个基于Python开发并且依赖jinja2模板和Werkzeug WSGI服务的一个微型框架,对于Werkzeug本质是Socket服务端,其用于接收http请求并对 ...
- 服务器迁移部署OmsWeb
绑定 基本设置 高级设置