1059 Prime Factors (25分)

1. 题目

2. 思路

先求解出int范围内的所有素数,把输入x分别对素数表中素数取余,判断是否为0,如果为0继续除该素数知道余数不是0,遍历到sqrt(x)就够了

3. 注意点

输入数为1的情况, 输出1=1

4. 代码

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std; #define MAXN 100000 // 长度10够用的,前10个素数的乘积已经超过int了
struct fra{
int x;
int n;
}f[10]; void show(fra a[], int size, int x){
printf("%d=", x);
int i = 0;
for(i=0;i<size-1;i++){
if(a[i].n == 1){
printf("%d*", a[i].x);
}else{
printf("%d^%d*", a[i].x, a[i].n);
}
}
if(a[i].n == 1){
printf("%d", a[i].x);
}else{
printf("%d^%d", a[i].x, a[i].n);
}
} bool isPrime[MAXN];
int prime[MAXN]; void init(){
int count = 0;
for(int i=2;i<=MAXN;i++){
if(isPrime[i] == false){
prime[count++] = i;
for(int j=i+i;j<MAXN;j+=i){
isPrime[j] = true;
}
}
}
} int main(){
int x;
init();
scanf("%d", &x);
int x_copy = x;
if(x == 1){
printf("1=1");
return 0;
}
int count = 0;
int sqr = sqrt(x);
for(int i=0;i<sqr;i++){
if(x % prime[i] == 0){
f[count].x = prime[i];
f[count].n = 1;
x/=prime[i];
while(x % prime[i] == 0){
f[count].n++;
x/=prime[i];
}
count++;
}
if(x == 1){
break;
}
}
if(x != 1){
f[count].x = x;
f[count].n = 1;
count++;
}
show(f, count, x_copy);
return 0;
}

1059 Prime Factors (25分)的更多相关文章

  1. 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 ...

  2. 【PAT甲级】1059 Prime Factors (25 分)

    题意: 输入一个正整数N(范围为long int),输出它等于哪些质数的乘积. trick: 如果N为1,直接输出1即可,数据点3存在这样的数据. 如果N本身是一个质数,直接输出它等于自己即可,数据点 ...

  3. PAT 1059. Prime Factors (25) 质因子分解

    题目链接 http://www.patest.cn/contests/pat-a-practise/1059 Given any positive integer N, you are suppose ...

  4. 1059. Prime Factors (25)

    时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HE, Qinming Given any positive integer N, y ...

  5. 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 ...

  6. PAT甲题题解-1059. Prime Factors (25)-素数筛选法

    用素数筛选法即可. 范围long int,其实大小范围和int一样,一开始以为是指long long,想这就麻烦了该怎么弄. 而现在其实就是int的范围,那难度档次就不一样了,瞬间变成水题一枚,因为i ...

  7. PAT (Advanced Level) 1059. Prime Factors (25)

    素因子分解. #include<iostream> #include<cstring> #include<cmath> #include<algorithm& ...

  8. pat1059. Prime Factors (25)

    1059. Prime Factors (25) 时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HE, Qinming Given ...

  9. PAT 1059 Prime Factors[难]

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

随机推荐

  1. linux命令su,sudo 使用和区别

    1- sudo 与 su 两个命令的最大区别是: sudo 命令需要输入当前用户的密码,su 命令需要输入 root 用户的密码.另外一个区别是其默认行为.sudo 命令只允许使用提升的权限运行单个命 ...

  2. 洛谷【P5004 专心OI - 跳房子】 题解

    题目链接 https://www.luogu.org/problem/P5004 洛谷 P5004 专心OI - 跳房子 Imakf有一天参加了PINO 2017 PJ组,他突然看见最后一道题 他十分 ...

  3. nat123+nginx实现外网访问本机IIS发布的系统

    故事开端(前因) 嗯,内网其实是校园网络,服务器呢,不是阿里云.腾讯云之类的云服务器,而是自己正在码字的笔记本电脑:有公网IP吗?没有!校园IP分配的IP固定不?不固定,动态分配的,额~~~. 我想想 ...

  4. GitHub概述

    1 引言 GitHub是为开发者提供Git仓库的托管服务,是一个让开发者与同事.同学及相同兴趣爱好者共享代码的完美场所. GitHub公司总部位于美国旧金山,拥有一只似章鱼又似猫的吉祥物. GitHu ...

  5. 安装oracle11gR2

    一.通过百度云等途径下载oracle11gR2 二.安装步骤 1,解压缩文件,将两个压缩文件一起解压. 2,在setup.exe右键--属性--兼容性,勾“以管理员身份运行此程序”. 3,双击“set ...

  6. linux--后端项目部署

    nginx + uwsgi + crm + mysql + virtualenv + supervisor项目部署 1.后端整起,用uwsgi启动crm 2.创建一个新的虚拟环境,用于运行crm新业务 ...

  7. Chrome浏览器控制台报Refused to get unsafe header "XXX"的错误

    最近在调试后端下载的接口时在浏览器的控制台中发现了红色的错误信息,例如Refused to get unsafe header "XXX":前端是采用XMLHttpRequest对 ...

  8. eclipse中配置maven环境

    一.配置setting.xml文件 1.首先将下载好的maven打开,打开文件夹,首先就需要对maven安装目录下有个config文件夹,在文件夹下有settings.xml文件.settings里面 ...

  9. 如何在K3查找BOS单据在哪个子系统中

    select FFunctionID,* from ICClassType where FName_CHS like '%采购订单%'select FSubSysID,* from t_DataFlo ...

  10. windows 服务启动外部程序

    服务使用Process启动外部程序没窗体 在WinXP和Win2003环境中,安装服务后,右键单击服务“属性”-“登录”选项卡-选择“本地系统帐户”并勾选“允许服务与桌面交互”即可. 在Win7及以后 ...