1059 Prime Factors (25分)
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分)的更多相关文章
- 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 ...
- 【PAT甲级】1059 Prime Factors (25 分)
题意: 输入一个正整数N(范围为long int),输出它等于哪些质数的乘积. trick: 如果N为1,直接输出1即可,数据点3存在这样的数据. 如果N本身是一个质数,直接输出它等于自己即可,数据点 ...
- PAT 1059. Prime Factors (25) 质因子分解
题目链接 http://www.patest.cn/contests/pat-a-practise/1059 Given any positive integer N, you are suppose ...
- 1059. Prime Factors (25)
时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HE, Qinming Given any positive integer N, y ...
- 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甲题题解-1059. Prime Factors (25)-素数筛选法
用素数筛选法即可. 范围long int,其实大小范围和int一样,一开始以为是指long long,想这就麻烦了该怎么弄. 而现在其实就是int的范围,那难度档次就不一样了,瞬间变成水题一枚,因为i ...
- PAT (Advanced Level) 1059. Prime Factors (25)
素因子分解. #include<iostream> #include<cstring> #include<cmath> #include<algorithm& ...
- pat1059. Prime Factors (25)
1059. Prime Factors (25) 时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HE, Qinming Given ...
- PAT 1059 Prime Factors[难]
1059 Prime Factors (25 分) Given any positive integer N, you are supposed to find all of its prime fa ...
随机推荐
- linux查看防火墙状态和对外开放的端口状态
1.查看防火墙状态 查看防火墙状态 systemctl status firewalld 开启防火墙 systemctl start firewalld ...
- H5_0009:关于HTML5中Canvas的宽、高设置问题
关于HTML5中Canvas的宽.高设置问题 Canvas元素默认宽 300px, 高 150px, 设置其宽高可以使用如下方法(不会被拉伸): 方法一: <canvas widt ...
- JavaScript 15 Ajax异步登陆
/** * Created by KING on 2017/11/28. */ var g_xhr_ui;var g_xhr_login;var g_id; $(document).ready(fun ...
- How to Install Oracle Java 11 on Ubuntu 18.04 LTS (Bionic) Written by Rahul, Updated on April 3, 20
本文系转载备份 请阅读点击下面链接阅读原文以获取更佳地阅读体验.谢谢. How to Install Oracle Java 11 on Ubuntu 18.04 LTS (Bionic) Writt ...
- CSS的文本样式
CSS的文本样式 1.颜色 2.文本对齐方式 3.首行缩进 4.行高 5.装饰 1. 文本位置 居中: text-align: center; 靠左: text-align: left; 靠右: te ...
- Android Studio阶段性学习总结_1
这半个月一直在学习Android方面的知识,对Android开发有了一个基本的认识,学会了Android studio的基本操作. 在建立第一个Android studio项目时,我遇到了很大的阻碍, ...
- windows上快速新建1g的文件
在E盘新建1g文件 fsutil file createnew d:\b.txt 1073741824
- 题解【AcWing883】高斯消元解线性方程组
题面 高斯消元模板题. 这里直接讲述一下高斯消元的算法流程: 枚举每一列 \(c\): 找到第 \(c\) 列绝对值最大的一行: 将这一行换到最上面: 将该行的第一个数变成 \(1\): 将下面所有行 ...
- 我的第一个Maven Helloworld
使用MAVEN创建项目可以有两种方式进行创建,第一种,使用mvn命令创建Maven项目,第二种,使用eclipse创建Maven项目. 在创建Maven项目之前,我们需要安装maven,并和配置JDK ...
- 1307E - Cow and Treats 二分 枚举边界 容斥
1307E - Cow and Treats 题意 有一排给定口味的草,并且给m头牛,每个牛都只吃一种口味的草,并且要吃给定数量个.现在可以安排牛从两边出发,方向向另一方向进发,每次路过符合他口味的草 ...