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. JavaDay10(下)

    生产者消费者问题 问题描述 有两个进程:一组生产者进程和一组消费者进程共享一个初始为空.固定大小为n的缓存(缓冲区).生产者的工作是制造一段数据,只有缓冲区没满时,生产者才能把消息放入到缓冲区,否则必 ...

  2. 题解【AcWing272】最长公共上升子序列

    题面 一道线性 DP 好题. 设 \(dp_{i,j}\) 表示在所有 \(a_{1\dots i}\),\(b_{1\dots j}\) 的子序列中,以 \(b_j\) 结尾的最长公共上升子序列的最 ...

  3. vue里使用element饿了么的el-menu+vue-router实现路由跳转的两种方法

    最近准备写一个echarts的可视化展示案例,首先用vue-cli3创建了一个项目(好像vue-cli4也出来,感觉变化不大,就没升级了) 然后,开始配置路由↓下面是我的router.js文件 imp ...

  4. 使用ResXmanager实现多语言[转]

    1 多國語系 - 使用RESXMANAGER管理資源檔 1.1 前言 在實作多國語系網站時,針對靜態文字多會使用資源檔(Resource)來對應出各語系所需顯示的內容.由於資源檔可能會依照特定結構放式 ...

  5. canvas特效-文字粒子

    具体的效果 https://www.ui.cn/detail/393461.html 实现类似的动效 要求: 1.文字内容可以自定义 2.粒子的运动轨迹 是曲线,返回是按照 原运动轨迹 3.粒子 堆叠 ...

  6. js对象赋值

    看到一道题: 根据包名,在指定空间中创建对象 效果 namespace({a: {test: 1, b: 2}}, 'a.b.c.d') 结果 {a: {test: 1, b: {c: {d: {}} ...

  7. Bootstrap资料

    Bootstrap手册  : https://www.jqhtml.com/bootstraps-syntaxhigh/index.html 中文文档 :https://v3.bootcss.com/ ...

  8. 【Python】表白程序

     程序链接:https://www.lanzous.com/i8xj5mh # 打包操作 # 安装pyinstaller # cmd输入 pip install pyinstaller # shift ...

  9. hadoop 配置信息记录

    ssh-keygen  -t   rsa   -P  '' 192.168.157.148 hadoop01192.168.157.149 hadoop02 mkdir  /root/hadoopmk ...

  10. JS高级---识别正则表达式是否匹配

    识别正则表达式是否匹配 console.log(/[a-zA-Z]+/.test("hello")); console.log(/./.test("除了回车换行以为的任意 ...