点击打开链接

注意:

1.对于输入为1的情况要特判

2.factor在int范围内开到10就足够

3.sqrt是范围,由于n后面被修改故范围要单写,且sqrt是double

int sqr=sqrt(1.0*n);

4.按照prime中的素数寻找时,当n==1是要退出

5.对于本身是素数的寻找过程,由于寻找范围只在sqrt内,因此循环过程后n不变,因此要特判

#include <cstdio>
#include <math.h>
#define MAX 100000
int prime[MAX];
bool p[MAX]={0};
int cnt=0;
void findp(){for(int i=2;i<MAX;i++){if(p[i]==false){prime[cnt++]=i;for(int j=i+i;j<MAX;j+=i){p[j]=true;}}}
}
struct factor{int x,cnt;factor(){x=1;cnt=0;}
}fac[10];
int main(){freopen("in.txt","r",stdin);findp();int n;while(scanf("%d",&n)!=EOF){//立刻打印,后面n变了if(n==1){printf("1=1\n");}else{//遍历次数要单独写,因为后面n会变化//sqrt中时double,因此要写成sqrt(1.0*n)printf("%d=",n);int sqr=(int)sqrt(1.0*n);int facnt=0;for(int i=0;prime[i]<sqr && i<cnt;i++){if(n%prime[i]==0){fac[facnt].x=prime[i];while(n%prime[i]==0){fac[facnt].cnt++;n/=prime[i];}facnt++;}//及时退出,节省时间if(n==1){break;}      }//对于本身是质因数的数字上述过程后,n不发生变化,因此只要写入n即可if(n!=1){fac[facnt].x=n;fac[facnt].cnt=1;facnt++;}for(int i=0;i<facnt-1;i++){if(fac[i].cnt==1){printf("%d*",fac[i].x);}else{printf("%d^%d*",fac[i].x,fac[i].cnt);}}if(fac[facnt-1].cnt==1){printf("%d\n",fac[facnt-1].x);}else{printf("%d^%d\n",fac[facnt-1].x,fac[facnt-1].cnt);}}}return 0;
}

1059. Prime Factors (25)--taste相关推荐

  1. 1059. Prime Factors (25)

    题目如下: Given any positive integer N, you are supposed to find all of its prime factors, and write the ...

  2. 【题目分析】1059 Prime Factors (25 分)

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Given any positive integer N, you are supposed to find all of its ...

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

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

  4. 1059 Prime Factors (25 分)【难度: 一般 / 知识点: 分解质因子 】

    注意: 判断1000000007这种只有一个质数特别大的情况 #include<bits/stdc++.h> using namespace std; map<int,int> ...

  5. 1059 Prime Factors(25 分)(cj)

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

  6. 1059 Prime Factors

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

  7. PAT 1059 Prime Factors[难]

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

  8. PAT甲级1059 Prime Factors :[C++题解]分解质因子

    文章目录 题目分析 题目链接 题目分析 暴力求质因数 下面i就是质因子,s是质因子i的阶数. 暴力的时间复杂度O(n),会超时 void divide(int n){for(int i=2;i< ...

  9. 1059 Prime Factors(25 分)

    Given any positive integer N, you are supposed to find all of its prime factors, and write them in t ...

最新文章

  1. 奇点云发布三大无人零售终端产品,“云”“端”结合赋能零售商
  2. 新年来临,给大家送上机器学习,人工智能相关书籍,这可能是中奖率最高的一次送书活动...
  3. 华为云总裁郑叶来:易获取、用得起、方便用的算力是人工智能发展的关键
  4. Appium之Hybrid APP混合应用测试
  5. 中国最大的python社区-在python数据框中的每一行中查找最大值
  6. VideoTool之FFmpeg:FFmpeg的简介、安装、使用方法之详细攻略
  7. 启明云端用一张表格带你了解sigmastar SSD20x系列的区别!帮助你快速完成选型!
  8. 印度孟买机器人餐厅_宝莱坞机器人2.0:重生归来一起来看印度神片!
  9. 【详解】CPU执行算术运算或逻辑运算时,常将源操作数和结果暂存在()中
  10. mongodb添加多条数据_分析了一万多条拼车数据,看看北上广深的各位都回哪过年...
  11. 【计算机网络】网络基础
  12. php前端路由权限,SaaS-前端权限控制
  13. OpenCV图像识别初探-50行代码教机器玩2D游戏
  14. mybatis问题。foreach循环遍历数组报错情况,及其解决方法
  15. 13.Linux 高性能服务器编程 --- 多进程编程
  16. 【C++】 53_被遗弃的多重继承 (上)
  17. Atitti 存储引擎支持的国内点与特性attilax总结
  18. 【建模算法】基于遗传算法求解TSP问题(Python实现)
  19. 【一起学加密4】一次性密码本
  20. ifix 读写mysql_[转载]vb6读取ifix实时数据库和历史数据库

热门文章

  1. PHP 第三方登录 (QQ登录)
  2. java显示多个地区时钟_Java实现的动态数字时钟功能示例【显示世界时间】
  3. java显示多个地区时钟_妙用Java多线程机制实现多个时区时钟显示
  4. Mina中的区块证明
  5. 云计算初探2-使用云服务器快速搭建论坛
  6. [论文解读 2021-TIP] Revisiting Shadow Detection: A New Benchmark Dataset for Complex World
  7. 数据基础---mysql数据库操作(一)---基础操作
  8. 英特尔、广东联通、广和通联合发布定制物联网模块产品“沃芯”
  9. 筛子点数相同概率—以四个骰子为例;三个点数相同;两个点数相同;顺子概率
  10. idea添加web3j 依赖