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. Spring学习笔记-高级装配-03

    主要内容: ●Spring profile ●条件化的bean声明 ●自动装配与歧义性 ● Spring表达式语言 本章介绍一些高级的装配技术,可实现更为高级的装配功能. 环境与profile 软件开 ...

  2. 安全 - CORS(脚本请求等)

    功能概述 出于安全原因,浏览器限制从脚本内发起的跨域HTTP请求 或 拦截了跨域请求的结果. 例如,XMLHttpRequest和Fetch API遵循同源策略. 这意味着使用这些API的Web应用程 ...

  3. ArcMap 导入 CGCS2000 点坐标数据

    最近开始用 ArcGIS 做管网项目,第一步就接触到 CGCS2000 坐标系数据,要把管网的数据给导入进来, 一开始不知道是哪个坐标系,试了半天,都偏到国外去了,最后在一篇博客里找到了类似的管网数据 ...

  4. AduSkin - UI

    追求极致,永臻完美 A Beautiful WPF Control UI 一款简单漂亮的WPF UI,融合部分开源框架的组件,为个人定制的UI,可供学者参考. Nuget 搜索"AduSki ...

  5. C 库函数 - sprintf()

    C 库函数 - sprintf() C 标准库 - <stdio.h> 描述 C 库函数 int sprintf(char *str, const char *format, ...) 发 ...

  6. C 库函数 - modf()

    C 库函数 - modf() C 标准库 - <math.h> 描述 C 库函数 double modf(double x, double *integer) 返回值为小数部分(小数点后的 ...

  7. LeetCode136. 只出现一次的数字(异或)

    给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次.找出那个只出现了一次的元素. 说明: 你的算法应该具有线性时间复杂度. 你可以不使用额外空间来实现吗? 示例 1: 输入: [ ...

  8. [AHOI2002] 芝麻开门 - 数论

    求 \(n^k\) 的因子和, \(n \leq 2^{16}, k \leq 20\) Solution \[\prod_i \frac{p_i^{q_ik+1}-1}{p_i-1}\] #incl ...

  9. shell登录 脚本 expect

    作用 工作中,我们运行命令.脚本或程序时,这些命令.脚本或程序都需要从终端输入某些继续运行的指令,而这些输入都需要人为的手工进行. 利用expect,则可以根据程序的提示,模拟标准输入提供给程序,从而 ...

  10. SQL四种语言:DDL,DML,DCL,TCL 的区别

    1.DDL(Data Definition Language)数据库定义语言statements are used to define the database structure or schema ...