这是一道素数因子分解的问题:

1.先打印素数表出来,以便后期使用,素数表的大小就是10^5级别就可以,因为输入的数是long int(即就是int而已),大小最大21亿(10^10量级的),我们这里素数范围只需要sqrt(10^10)=10^5就行

(使用2-根号n依此取模的方式  或者  使用筛选法),我下面是使用筛选法直接打印素数表的

2.在sqrt_n范围的素数逐个进行一到多次取模运算判断是否是否为因子以及因子的阶数,并将结果存在struct factor的数组fac中

3.如若最后剩下的n不是1(因为n不断取模后,最后如果在sqrt_n范围内就能解决,n就会变为1,不需要再操作什么),即将n放入struct factor的数组fac中,阶数为1

4.最后就是按照格式要求打印就行,现在用着printf,scanf怎么感觉比以前cin,cout还要舒服,不知道是怎么回事哈哈

小坑:1.素数表是从2开始的,所以打印,判断素数什么的循环都要从i=2开始,不然会陷入死循环

   2.第三部判断n为1,不是n为0

#include<iostream>
#include<stdio.h>
#include<cmath>
using namespace std;
int isprime[]; //0是1不是 void find_prime(int n){
for(int i=;i<=n;++i){ //从2开始
if(isprime[i]==){
for(int j=i+i;j<=n;j+=i){
isprime[j]=;
}
}
}
} struct factor{
int f;
int p;
}fac[]; int main(){
freopen("in.txt","r",stdin); int input;
scanf("%d",&input);
if(input==){ //特殊情况为1
printf("1=1");
return ;
}
int n=input; int sqrt_n=sqrt(n);
find_prime(sqrt_n); int posi=;
for(int i=;i<=sqrt_n;++i){ //一定要从2开始
if(isprime[i]==){
int count=;
while(n!= && n%i==){
count++;
n/=i;
}
if(count!=){
fac[posi].f=i;
fac[posi].p=count;
posi++;
}
if(n==)
break;
}
}
if(n!=){ //不等于1
fac[posi].f=n;
fac[posi].p=;
}
printf("%d=",input);
for(int i=;;++i){
if(fac[i].p==)
break;
if(i!=)
printf("*");
printf("%d",fac[i].f);
if(fac[i].p!=)
printf("^%d",fac[i].p);
} return ;
}

PAT_A1059的更多相关文章

  1. PAT_A1059#Prime Factors

    Source: PAT A1059 Prime Factors (25 分) Description: Given any positive integer N, you are supposed t ...

随机推荐

  1. 【linux基础】Ubuntu16.04桌面突然卡住怎么办?

    使用Ctrl+Alt+F1先进入命令行模式,然后根据问题进行相应的操作. re 1. Ubuntu16.04桌面突然卡住怎么办; 2. Ubuntu下图形界面卡死解决办法; end

  2. [LeetCode] 361. Bomb Enemy 炸敌人

    Given a 2D grid, each cell is either a wall 'W', an enemy 'E' or empty '0' (the number zero), return ...

  3. 一键安装docker-ce

    curl https://get.docker.com |env CHANNEL=stable sudo sh -s docker --mirror Aliyun

  4. SpringBoot系列教程JPA之delete使用姿势详解

    原文: 190702-SpringBoot系列教程JPA之delete使用姿势详解 常见db中的四个操作curd,前面的几篇博文分别介绍了insert,update,接下来我们看下delete的使用姿 ...

  5. SpringBoot系列教程JPA之update使用姿势

    原文: 190623-SpringBoot系列教程JPA之update使用姿势 上面两篇博文拉开了jpa使用姿势的面纱一角,接下来我们继续往下扯,数据插入db之后,并不是说就一层不变了,就好比我在银行 ...

  6. springboot集成mybatisplus小例子

    集成mybatisplus后,简单的CRUD就不用写了,如果没有特别的sql,就可以不用mapper的xml文件的. 目录 pom.xml文件 <?xml version="1.0&q ...

  7. 09 Spring的依赖注入

    1.依赖注入(Dependency Injection) (1)IOC的作用: 降低程序间的耦合(依赖关系)(2)依赖关系的管理: 以后都交给spring来维护 在当前类需要用到其他类的对象,由spr ...

  8. Spring全家桶注解一览(精选)

    废话 最近想整理一波Spring注解相关的文章,虽然写CURD就只涉及到那些常用的注解.但是笔者我也想去了解一下其他注解,丰富下自己的知识面(提升一下逼格!). 就想在网上搜了半天,好像大家的都差不多 ...

  9. linux系统状态脚本

    #!/bin/bashprintf "%10s\n" "##主机名##"printf "%-10s\n" "$(hostname) ...

  10. Keil 5出现Error: L6218E: Undefined symbol解决方法

    首先列出网上百度到比较好的blog: blog1:https://blog.csdn.net/super_demo/article/details/32131379 总结了代码中可能因为几种初级或者粗 ...